尝试过滤字段时出错

时间:2017-10-20 23:40:56

标签: jquery html ruby-on-rails

我正在尝试根据之前的选择过滤选择字段,但它不起作用,它给了我一个错误:

  

未捕获错误:语法错误,无法识别的表达式:   optgroup [label =门到门销售]

<select id="affiliation" name="affiliate[industry]">
    <option value="">Industry</option>
    <option value="3">Auto Sales</option>
    <option value="1">Door-to-Door Sales</option>
    <option value="10">Independent</option>
    <option value="11">Mortgage Officer</option>
    <option value="12">Real Estate Agent</option>
    <option value="13">Seminar Services</option>
  </select>

  <select id="companies" name="affiliate[parent_affiliate_id]">
    <option value="">Company</option>
    <optgroup label="Auto Sales"></optgroup>
    <optgroup label="Door-to-Door Sales ">
    <option value="3">Vivint Smart Home</option>
    <option value="4">Vivint Solar</option>
    </optgroup>
    <optgroup label="Independent"></optgroup>
    <optgroup label="Mortgage Officer"></optgroup>
    <optgroup label="Real Estate Agent"></optgroup>
    <optgroup label="Seminar Services">
    <option value="5">RoryRicord.com</option>
    </optgroup>
  </select>

我得到的jQuery基于本教程https://www.rubyplus.com/articles/3691-Dynamic-Select-Menus-in-Rails-5

jQuery(function() {
    var companies;
    companies = $('#companies').html();
    console.log(companies);
    return $('#affiliation').change(function() {
        var industry, options;
        industry = $('#affiliation :selected').text();
        options = $(companies).filter("optgroup[label=" + industry + "]").html();
        console.log(options);
        if (options) {
            return $('#companies').html(options);
        } else {
            return $('#companies').empty();
        }
    });
});

2 个答案:

答案 0 :(得分:1)

我猜它错过了双引号。所以我将双引号更改为单引号并在after [和之前]

中添加双引号
options = $(companies).filter('optgroup[label="' + industry + '"]').html();

答案 1 :(得分:0)

您的浏览器似乎无法渲染当前的jquery。 试试

options = $(companies).filter("optgroup<label=" + industry + ">").html()