【发布时间】:2014-02-18 09:33:48
【问题描述】:
我在我的项目中使用Jquery Select2。加载页面时,我想在单个输入字段中保留默认值。我通过设置 initSelection 来尝试这个,同时我正在像这样启动 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 选项时,这非常有效。如何对单个隐藏字段执行此操作?
感谢和问候!
【问题讨论】:
标签: jquery-select2