Sha*_*der 6 javascript jquery jquery-plugins jquery-select2
我正在使用select2插件来加载远程数据.我正在使用一个返回JSON数据的aspx页面,并将其分配给select2插件.用户从select2文本框中选择一些值后,我强制页面回发.回发后我使用以下代码重新加载以在select2文本框中设置文本.
var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
$('#e6').select2('val', '123');
Run Code Online (Sandbox Code Playgroud)
但系统抛出以下错误: cannot call val() if initSelection() is not defined
即使我定义了init,我也无法设置值.我正在使用以下代码.请帮我在回发后设置select2文本框上的值.
$(document).ready(function () {
$("#e6").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "data.aspx",
dataType: 'json',
quietMillis: 1000,
data: function (term, page) {
return {
name: term
};
},
initSelection: function (element, callback) {
var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
callback(data);
},
results: function (data) {
var results = [];
$.each(data, function (index, item) {
results.push({
id: item['Email'],
text: item['PatientID']
});
});
return {
results: results
};
},
},
});
});
window.onload = function () {
var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
//When this is called system is throwing error
//This code is required to show the value in select2 textbox after the post back
$('#e6').select2('val', data);
}
$(document).ready(function () {
$("#e6").on("select2-selecting", function (e) {
//alert("selecting val=" + e.val + " choice=" + JSON.stringify(e.choice));
var id = document.getElementById('<%= savebtn.ClientID %>');
document.getElementById('<%= hdnFld.ClientID %>').value = e.val;
id.value = e.val;
//causes post back
id.click();
});
});
Run Code Online (Sandbox Code Playgroud)
小智 5
你犯了错误!我遇到了这个问题而且我看到了你的问题.然后我读了Select2的API文档和fount,我错了.Pleas把initSelection和ajax.like放在一起:
$("#e6").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
initSelection: function (element, callback) {
var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
callback(data);
},
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "data.aspx",
dataType: 'json',
quietMillis: 1000,
data: function (term, page) {
return {
name: term
};
},
results: function (data) {
var results = [];
$.each(data, function (index, item) {
results.push({
id: item['Email'],
text: item['PatientID']
});
});
return {
results: results
};
},
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7177 次 |
| 最近记录: |