我在我的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