Dynamic select2 not firing change event

☆樱花仙子☆ 提交于 2019-12-03 09:37:20
Mikhail Chernykh

Select2 has only 2 events, open and change (http://select2.github.io/select2/#events), you are able to add listeners only to them. You can use open event instead of focus for <select> element. And please don't use live() method, as it is deprecated. Use on() instead.

var currentData;
$(".autosubmit select").on("open", function() {
    currentData = $(this).val();
});
$(".autosubmit input").on("focus", function() {
    currentData = $(this).val();
});
$(".autosubmit input, .autosubmit select").on("change", function() {
    var $this = $(this);
    console.log('autosubmitting');
    if (!currentData || currentData != $this.val()) {
        $($this.get(0).form).ajaxSubmit(function (response, status, xhr, $form) {
            currentData = "";
        });
    }
});

Here is the Fiddle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!