【问题标题】:error trying to filter a field尝试过滤字段时出错
【发布时间】:2017-10-20 23:40:56
【问题描述】:

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

未捕获的错误:语法错误,无法识别的表达式: 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();
        }
    });
});

【问题讨论】:

    标签: jquery html ruby-on-rails


    【解决方案1】:

    我猜它缺少双引号。所以我将双引号改为单引号,并在[和之前]之后添加双引号

    options = $(companies).filter('optgroup[label="' + industry + '"]').html();
    

    【讨论】:

      【解决方案2】:

      您的浏览器似乎无法呈现当前的 jquery。 试试

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

      【讨论】:

      • 它没有用。我检查了一下,它说at HTMLSelectElement.&lt;anonymous&gt; (VM3376 select-button.js:8) 会不会是我弄乱了其中一个名称,它不知道要更改什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 2021-04-12
      • 1970-01-01
      • 2018-01-17
      • 2021-04-05
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多