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

前端之家收集整理的这篇文章主要介绍了javascript – 未捕获的TypeError:无法设置未定义的属性“已选择”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的 JSFiddle

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

未捕获的TypeError:无法设置未定义的admin.js的属性“selected”:1000
Chosen.result_select admin.js:1000
Chosen.search_results_mouseup admin.js:1000
(匿名函数)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")
    });
});

解决方法

这是一个老问题,但我最近得到了同样的错误.
我修复了这个添加空选项标签,如下所示:
<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>

猜你在找的JavaScript相关文章