我对 jQuery 的 Select2 有疑问。
页面加载时,如果点击搜索结果,它会选择并触发 onchange 事件,但仅限第一次。
如果我再搜索一次,就不会了。
这是我的代码(它是基于 Ajax 的搜索):
jQuery('#search_code').select2({
'width': '600px',
'tokenSeparators': [',', ' '],
'closeOnSelect': false,
'placeholder': 'scan or type a SKU or product or ESN',
'minimumInputLength': 1,
'ajax': {
'url': 'lookProduct',
'dataType': 'json',
'type': 'POST',
'data': function (term, page) {
return {
barcode: term,
page_limit: 3,
page: page
};
},
'results': function (data, page) {
return {
results: data
};
}
}
}).on('change', function (e) {
var str = $("#s2id_search_code .select2-choice span").text();
DOSelectAjaxProd(this.value, str);
//this.value
}).on('select', function (e) {
console.log("select");
});
最佳答案
这就是我正在使用的:
$("#search_code").live('change', function(){
alert(this.value)
});
对于最新的 jQuery 用户,这个应该可以工作:
$(document.body).on("change","#search_code",function(){
alert(this.value);
});
关于jquery - select2 onchange 事件仅有效一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731694/