我在我的MVC项目中使用Select2 Dropdown下拉列表,如下所示。尽管我可以直接通过AJAX传递“查询”参数,但我无法在下面的数据部分中获取所选文本或下拉列表的值。如何得到它?无法获得Select2下拉列表的选定值
@Html.DropDownListFor(x => x.StudentId, Enumerable.Empty<SelectListItem>(),
new { style ="width: 100%;" })
$("#StudentId").select2({
multiple: false,
placeholder: "Select",
allowClear: true,
tags: true,
ajax: {
url: '/Grade/StudentLookup',
dataType: 'json',
delay: 250,
data: function (params) {
return {
//q: params.Name, // search term
//page: params.page
query : 'test' //this parameter can be passed via AJAX
//!!! but I cannot get the selected text or value of dropdownlist
};
},
processResults: function (data, page) {
var newData = [];
$.each(data, function (index, item) {
newData.push({
id: item.Id, //id part present in data
text: item.Name //string to be displayed
});
});
return { results: newData };
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 0, //for listing all, set : 0
maximumInputLength: 20, // only allow terms up to 20 characters long
});
非常感谢您的完美答案。我错过了一些东西,无法获得AJAX中** data **部分的选定值。我怎么能在那里抓到它? –
我创建了一个变量var selectedText =“”;然后在函数onSelect(evt){}中设置** selectedText = $(this).val(); **然后尝试将此值分配给数据部分中的** query:selectedText **,但不能设置查询参数。任何想法? –
所以你想将输入文本作为参数传递给ajax调用? – DavidDomain