我有一個問題與jQuery的Select2。select2 onchange事件只能工作一次
當頁面加載時,如果O單擊搜索結果,它將選擇並觸發事件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");
});
你在調試器中試過這個嗎?特別是當第一個onchange事件發生時,你是否收到任何錯誤消息? – Lochemage
嘗試'$(document).on('change','span#search_code',function(...'並告訴它是否有效。如果#search_code是id到div或「元素」,則將span替換爲div –
是的,它是一個虛驚。我實際上是選擇相同的元素,所以它不會觸發在這種情況下的變化事件... – PartySoft