5
如何在用戶選擇的下拉列表項中設置?jQuery Select2 Ajax - 如何設置值(initSelection)
場景:
1.用戶在形式上不會進入所有所需的值
2.點擊發送。
3.頁面刷新並且未選擇下拉列表中的值。 如何選擇值?
我有工作腳本檢索列表的數據。在Ajax調用
$('#userid').select2({
placeholder : " --- select ---",
minimumInputLength: 2,
ajax: {
url: "index.php?modul=getusers",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: function (term, page) {
return {
q: term,
page_limit: 10
};
},
results: function (data, page) {
return { results: data };
}
},
allowClear: true,
formatSelection: function(data) {
return data.text;
}
});
標準數據: { 「文」: 「示例文本」, 「ID」: 「1」}
輸入:
<input type="text" value="<? echo $_POST['userid']; ?>" class="input" id="userid" name="userid">
我嘗試添加以下代碼,但它不工作
initSelection: function(element, callback) {
var id=$(element).val();
if (id!=="") {
$.ajax("index.php?modul=getusersfriend&q="+id, {
dataType: "json"
}).done(function(data) { callback(data); });
}
},