简体   繁体   English

Uncaught TypeError: Cannot read property 'current' of null - Select2.js 选择任何选项

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

Select2 on dropdown always gives Uncaught TypeError: Cannot read property 'current' of null , when i select any data in select box it gives me error and it not call change event function下拉列表中的 Select2 总是给出Uncaught TypeError: Cannot read property 'current' of null ,当我 select select 框中的任何数据时它给我错误并且它不调用更改事件 function

html codes 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 codes to get data on initialize select2 and on change event handler用于获取有关initialize select2on change事件处理程序的数据的 Js 代码

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

It run fine only first time after that its always return Uncaught TypeError: Cannot read property 'current' of null in js...它只有第一次运行良好,之后它总是返回Uncaught TypeError: Cannot read property 'current' of null in js ...

I want change event work for every selection but now it runs only once我想为每个选择更改事件工作,但现在它只运行一次

help to solve this problem帮助解决这个问题

You should chain the select2() and val() functions in jquery instead of trying to use the select2() function with val as its parameter. 您应该在jquery中链接select2()和val()函数,而不是尝试使用val作为参数的select2()函数。

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

` `

You can Try This... 你可以尝试这个......

$(document).ready(function(){
    $(".select2").select2().on("change", function (e) {
        console.log("change",$(this).val());
    });
});

http://jsfiddle.net/ve3ntc6q/ http://jsfiddle.net/ve3ntc6q/

 var bindsonoSelect = function(){
        $("select#sonoDetails").on("change",function(){
            var me = $(this);
             var sono = me.select2("val");
            //var sono = me.select2().val();
            console.log(sono);
        }
    });
 }
bindsonoSelect ();

I have the same issue. 我有同样的问题。

I fixed it by making sure all JSON objects that I pass into my select2 is not null or empty. 我通过确保传递给select2的所有JSON对象都不为null或为空来修复它。 Then everything works fine. 一切正常。 This occurs on my Select 4.0.5 version. 这发生在我的Select 4.0.5版本上。

Try this .. This solved my problem试试这个..这解决了我的问题

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Select2.js错误:无法读取未定义的属性“长度” - Select2.js error: Cannot read property 'length' of undefined 未捕获的类型错误:无法在 magento 2.1 中读取 null 的属性“选择” - Uncaught TypeError: Cannot read property 'select' of null in magento 2.1 未捕获的TypeError:无法读取select2中null的属性“ replace” - Uncaught TypeError: Cannot read property 'replace' of null in select2 反应选择:未捕获的类型错误:无法读取 null 的属性“offsetTop” - React-select: Uncaught TypeError: Cannot read property 'offsetTop' of null 未捕获的TypeError:无法读取null的属性“ getAttribute”(Select2下拉列表) - Uncaught TypeError: Cannot read property 'getAttribute' of null (Select2 dropdown) 未捕获的TypeError:无法读取HTML选择的null属性'options' - Uncaught TypeError: Cannot read property 'options' of null for HTML select Enyo JS Uncaught TypeError:无法读取null的属性&#39;offsetWidth&#39; - Enyo JS Uncaught TypeError: Cannot read property 'offsetWidth' of null 奇怪的JS错误:未被捕获的TypeError:无法读取null的属性&#39;url&#39; - Strange JS Error: Uncaught TypeError: Cannot read property 'url' of null 未捕获的类型错误:无法读取 null 的属性“长度” - Chart.js - Uncaught TypeError: Cannot read property 'length' of null - Chart.js Simon.js:未捕获的TypeError:无法读取null的属性“样式” - Simon.js: Uncaught TypeError: Cannot read property 'style' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM