Jquery 正在删除select2中的选定项而不更新val()
我有一个输入控件,使用as multiselection select2Jquery 正在删除select2中的选定项而不更新val(),jquery,ajax,jquery-select2,Jquery,Ajax,Jquery Select2,我有一个输入控件,使用as multiselection select2 <input class="form-control no-padding cTagID" tabindex="3" id="cTagID" name="cTagID" style="border:none;" /> 问题是,如果用户选择一个项目,然后将其删除,它将保留为val(“#cTagID”)。在研究Select2.js脚本后,val()也将返回删除的项目。我发现select2在显示所选项目时会修剪i
<input class="form-control no-padding cTagID" tabindex="3" id="cTagID" name="cTagID" style="border:none;" />
问题是,如果用户选择一个项目,然后将其删除,它将保留为val(“#cTagID”)。在研究Select2.js脚本后,val()也将返回删除的项目。我发现select2在显示所选项目时会修剪id字段。但该值仍未修剪。因此,当删除选定项目时,select2尝试删除具有修剪id的项目。但找不到该项目,因为它与任何值都不匹配 注意:如果在分配给select2之前,数据源中的Id字符串为trimmed value,那么最好也使用trimmed value。请查看url:。在语句“$(this).valid();”之后添加“$(this).trigger('change');”,然后在之后尝试获取下拉列表的值。它应该会起作用。
$("#cTagID").select2({
placeholder: "Select a Tag",
allowClear: true,
width: '100%',
multiple: true,
maximumSelectionSize: 9,
escapeMarkup: function (text) { return text; },
minimumInputLength: 0,
ajax: {
url: '@Url.Action("myaction", "mycontroller")',
dataType: 'json',
quietMillis: 0,
cache: false,
type: "POST",
data: function (term, page) {
return {
q: term,
page: page,
listType: "mytype",
Searchterms: $("#iProjectId").val()
};
},
results: function (data, page) {
var more = (page * 30) < data.total_count; // whether or not there are more results available
return {
results: $.map(data.items, function (item) {
return {
text: item.text,
id: item.id
}
}),
more: more
}
},
cache: true
}
}).on("change", function () {
$(this).valid();
alert($("#cTagID").val()) // Problem is here ..Even if a selected item is removed by user it will still be available in val
});
{
"total_count":6905,
"items":[
{
"id":"(DB-227)-Q14-SL03 ",
"text":"(DB-227)-Q14-SL03 ~ E_ELOOP "
},
{
"id":"(DB-227)-Q14-SL04 ",
"text":"(DB-227)-Q14-SL04 ~ E_ELOOP "
},
{
"id":"011100-727 ",
"text":"011100-727 ~ E_OTP "
},
]
}