我有一个需要使用 select2 组件的网页。还需要在负载上显示选定的值。在我的 JS 文件中,我有两个构造
JS - 构造 1 用于选择/删除选项
$("#inp_select_linkproject").select2({
minimumInputLength: 2,
maximumSelectionLength: 1,
ajax: {
type : 'POST',
url: '../../ase.php',
dataType: 'json',
delay: 250,
data: function (term, page) {
return {
wildcardsearch: term, // search term
data_limit: 10,
data_offset: 0,
page_mode:"SELECT",
agent_id:$("#ipn_hdn_userid").val()
};
},
processResults: function (data, page) {
return { results: data.dataset};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
});
JS - Construct 2 用于餐饮负载
$.fn.getCurrentSelect2data = function(){
$("#inp_select_linkproject").val(null).trigger("change");
var $element = $('inp_select_linkproject').select2(); // the select element you are working with
var postFormData = {
'eucprid' : $("#ipn_hdn_eucprid").val()
};
var $request = $.ajax({
type : 'POST',
url: '../../ase_x.php',
data : postFormData,
dataType: 'json'
});
$request.then(function (data) {
// This assumes that the data comes back as an array of data objects
// The idea is that you are using the same callback as the old `initSelection`
console.log("rowselect,data0-"+data[0].text);
for (i=0; i<data.length; i++) {
$('#inp_select_linkproject').append($("<option/>", {
value: data[i].id,
text: data[i].text,
selected: true
}));
}
$('#inp_select_linkproject').trigger('change');
});
}
现在的问题是重复选择正在发生,并且重复次数会随着选择更多选项而增加。你能帮帮我吗?