未捕获的TypeError: Cannot read property 'current'在选择任何选项

Uncaught TypeError: Cannot read property 'current' of null - Select2.js on selection any option

本文关键字:current 选择 选项 任何 property TypeError read Cannot      更新时间:2023-09-26

下拉菜单上的select t2总是给出Uncaught TypeError: Cannot read property 'current' of null,当我在选择框中选择任何数据时,它会给我错误,它不会调用更改事件函数

html代码

 <select name="sono[]" id="sonoDetails" class="select2 form-control select2-hidden-accessible" tabindex="-1" aria-hidden="true">
    <option value="">Select SO No</option>
    <option value="5" data-bookingtime="2015-10-16 21:00:00">5</option>
    <option value="4" data-bookingtime="2015-10-17 20:15:00">4</option>                         
</select>

Js代码获取initialize select2on change事件处理程序的数据

 $(document).ready(function(){
    $(".select2").select2();
    $(document).on("change","select[name='sono[]']",function(){//
        var me = $(this);
        var sono = me.select2("val");
          console.log(sono);
    }); 
 });

它运行良好,只有第一次之后,它总是返回Uncaught TypeError: Cannot read property 'current' of null在js…

我想为每个选择更改事件工作,但现在它只运行一次

帮助解决这个问题

你应该在jquery中链接select2()和val()函数,而不是试图使用val作为参数的select2()函数。

$(document).ready(function(){
  $(document).on('change', "select[name='sono[]']", function() {
    var sono = $('.select2').select2().val()
    console.log(sono)
  })
})

"

你可以试试这个…

$(document).ready(function(){
    $(".select2").select2().on("change", function (e) {
        console.log("change",$(this).val());
    });
});
http://jsfiddle.net/ve3ntc6q/

我有同样的问题。

我通过确保我传递到我的select2的所有JSON对象不是null或空来修复它。然后一切都很好。这发生在我的Select 4.0.5版本

试试这个…这就解决了我的问题

$("select").change(function () {
...
});
with:
$("select").on("select2:select select2:unselecting", function () {
...
});