rails 5 javacript文件无法正常工作,因为

问题描述 投票:0回答:1

其他js文件运行正常,但

仅此js文件为

无法使用错误

此文件适用于保管箱过滤器

这是我的保管箱过滤器的JS代码

// Events
$(".dropdown-container")
    .on("click", ".dropdown-button", function () {
        $(this).siblings(".dropdown-list").toggle();
    })
    .on("input", ".dropdown-search", function () {
        var target = $(this);
        var dropdownList = target.closest(".dropdown-list");
        var search = target.val().toLowerCase();

        if (!search) {
            dropdownList.find("li").show();
            return false;
        }

        dropdownList.find("li").each(function () {
            var text = $(this).text().toLowerCase();
            var match = text.indexOf(search) > -1;
            $(this).toggle(match);
        });
    })
    .on("change", '[type="checkbox"]', function () {
        var container = $(this).closest(".dropdown-container");
        var numChecked = container.find('[type="checkbox"]:checked').length;
        container.find(".quantity").text(numChecked || "Any");
    });

// JSON of States for demo purposes
var usStates = [
    { name: "ALABAMA", abbreviation: "AL" },
    { name: "ALASKA", abbreviation: "AK" },
    { name: "AMERICAN SAMOA", abbreviation: "AS" },
    { name: "ARIZONA", abbreviation: "AZ" },
    { name: "ARKANSAS", abbreviation: "AR" },
    { name: "CALIFORNIA", abbreviation: "CA" },


];

**// <li> template
var stateTemplate = _.template(
    "<li>" +
        '<input name="<%= abbreviation %>" type="checkbox">' +
        '<label for="<%= abbreviation %>"><%= capName %></label>' +
        "</li>"
);**

// Populate list with states
_.each(usStates, function (s) {
    s.capName = _.startCase(s.name.toLowerCase());
    $("ul").append(stateTemplate(s));
});

这里是错误代码

undefined local variable or method `abbreviation' for #<#<Class:0x000056540969d760>:0x00007fd9f9803718>
Extracted source (around line #95):
93
94
95
96
97
98

var stateTemplate = _.template(
    "<li>" +
        '<input name="<%= abbreviation %>" type="checkbox">' +
        '<label for="<%= 

abbreviation %>"><%= capName %></label>' +
        "</li>"
);

这是因为rails

我是新手,所以...你们能帮我为我更改代码吗

codepen.io/rustybailey/pen/GJjvYB我从此处复制此代码,似乎也没有定义缩写,但它在那里工作但在我的rails项目中不起作用

javascript ruby-on-rails-5
1个回答
0
投票

您在哪里定义abbreviation?您可以尝试将其设置为实例变量@abbreviation = 'something',并可以像这样引用它<%= @abbreviation %>

© www.soinside.com 2019 - 2024. All rights reserved.