未捕获的TypeError:无法设置未定义的属性“已选择”

时间:2013-10-14 21:01:58

标签: javascript jquery jquery-chosen

这是我的JSFiddle

我的html和js在JSFiddle中运行良好。但它在我的rails应用程序中的控制台中给出了错误

Uncaught TypeError: Cannot set property 'selected' of undefined admin.js:1000 Chosen.result_select admin.js:1000 Chosen.search_results_mouseup admin.js:1000 (anonymous function) admin.js:1000 x.event.dispatch admin.js:6 v.handle

HTML

<div class="col-xs-3 equ">
       <select class = "car-select col-xs-10 col-sm-10 col-sm-offset-1 center boxalign" id = "brand">
          <option value="brand">Brand Select</option>
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes1">Mercedes</option>
          <option value="audi">Audi</option>
          <option value="volvo2">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
        </select>
    </div>

  <div class="col-xs-3 equ">
       <select class = "car-select col-xs-10 col-sm-10 col-sm-offset-1 center boxalign" id = "car">
          <option value="brand" class = "mercedes">Brand Select</option>
          <option value="volvo" class = "mercedes">Volvo</option>
          <option value="saab" class = "volvo">Saab</option>
          <option value="mercedes" class = "volvo">Mercedes</option>
          <option value="audi" class = "volvo">Audi</option>
          <option value="volvo" class = "mercedes">Volvo</option>
          <option value="saab" class = "mercedes">Saab</option>
          <option value="mercedes" class = "mercedes">Mercedes</option>
          <option value="audi" class = "mercedes">Audi</option>
        </select>
  </div>

的JavaScript

$(".car-select").chosen({disable_search_threshold: 10});
$(".car-select-deselect").chosen({allow_single_deselect:true}); 
  $(function() {
    $("#car").chained("#brand"); 
    $("#brand").trigger("liszt:updated");   
    $("#brand").bind("change", function(){
    $("#car").trigger("liszt:updated")
    });
});

2 个答案:

答案 0 :(得分:12)

这是一个老问题,但我最近得到了同样的错误。 我修复了这个添加空选项标签,如下所示:

<select class = "car-select col-xs-10 col-sm-10 col-sm-offset-1 center boxalign" id = "car">
      <option value=""></option>
      <option value="brand" class = "mercedes">Brand Select</option>
      <option value="volvo" class = "mercedes">Volvo</option>
      <option value="saab" class = "volvo">Saab</option>
      <option value="mercedes" class = "volvo">Mercedes</option>
      <option value="audi" class = "volvo">Audi</option>
      <option value="volvo" class = "mercedes">Volvo</option>
      <option value="saab" class = "mercedes">Saab</option>
      <option value="mercedes" class = "mercedes">Mercedes</option>
      <option value="audi" class = "mercedes">Audi</option>
</select>

答案 1 :(得分:0)

添加 <option disabled></option> 到你的列表将解决错误。