사용자가 선택한 드롭 다운 목록 항목의 설정 방법은 무엇입니까?jquery Select2 Ajax - 설정 값 (initSelection)
시나리오 :
1. 사용자는 형태로
2.를 보낸 모든 필요한 값을 입력하지.
3. 페이지가 새로 고침되고 드롭 다운 목록의 값이 선택되지 않습니다. 값을 선택하는 방법은 무엇입니까?
목록의 데이터를 검색하는 작업 스크립트가 있습니다. 아약스 호출
$('#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); });
}
},