1
我正在爲我的項目使用Jquery Select2。我想在加載頁面時在單個輸入字段中保留默認值。我試圖通過設置initSelection
而我開始這樣的select2組件。select2單個字段的默認值
$(document).ready(function() {
allowClear: true,
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
dataType: 'json',
url: "public/data.php",
data: function (term, page) {
return {
q: term, // search term
component: "map",
all_selected: $("#map-all").hasClass("active"),
parent_value: $("#city").val(),
child_value: ""
};
},
results: function (data, page) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
},
initSelection: function(element, callback) {
return $.getJSON("public/data.php?q="+element.val()+"&component=map&all_selected=false&parent_value=&child_value=", null, function(data) {
if ($.isFunction(callback)) {
return callback(data);
}
});
},
formatSelection: format,
formatResult: format,
});
但是,這不起作用,因爲它應該是。
但是,當我將multiple: true,
設置爲select2選項時,此功能完美無缺。我如何爲單隱藏領域做到這一點?
謝謝&關心!