我的html(mvc with razor)页面中有这个select2代码:
$('#QuickSearchState').select2({
minimumInputLength: 3,
width: 'resolve',
ajax: {
url: '@Url.Action("QuickSearchState", "DataCenter")',
contentType: 'application/json',
dataType: 'json',
type: 'POST',
traditional: true,
quietMillis: 400,
data: function(term, page) {
var data = {
term: term
};
return data;
},
results: function(data, page) {
return { results: data };
}
},
initSelection: function(element, callback) {
var data = { id: element.val(), text: element.val() };
callback(data);
},
formatResult: function(format) {
return format.label;
},
formatSelection: function(format) {
//this is a knockout view model
vmNewAddress.IdState(format.id);
vmNewAddress.StateName(format.stateName);
return format.label;
},
dropdownCssClass: "bigdrop",
escapeMarkup: function(m) { return m; }
});
但我在我的代码中有另一个选择,可以在此选择中设置状态,但我不知道如何将此值设置为该选择。
在我的HTML中我有这个:
<select class="width-xl" data-bind="options: vm.GivenLocationsForConnection, optionsText: 'DisplayFormat', value: SelectedLocation"></select> -> first select that can fill the second state search select with a state
@Html.Hidden("query", null, new { @id = "QuickSearchState", @class = "width-xl", placeholder = "Ej. Montevideo" }) -> second select, this is a search for states and selects a single state
答案 0 :(得分:0)
我不确定这是否是您想要的,但如果您只想在选择中选择一个值,您可以在下面执行
$("#QuickSearchState").select2("val", "<YOUR VALUE>");