Javascript 如果在select2插件中未定义initSelection(),则无法调用val()
我正在使用select2插件加载远程数据。我使用的是一个aspx页面,它返回JSON数据,并将其分配给select2插件。在用户从select2文本框中选择一些值后,我将强制页面回发。回发后,我使用以下代码重新加载以设置select2文本框中的文本Javascript 如果在select2插件中未定义initSelection(),则无法调用val(),javascript,jquery,jquery-plugins,jquery-select2,Javascript,Jquery,Jquery Plugins,Jquery Select2,我正在使用select2插件加载远程数据。我使用的是一个aspx页面,它返回JSON数据,并将其分配给select2插件。在用户从select2文本框中选择一些值后,我将强制页面回发。回发后,我使用以下代码重新加载以设置select2文本框中的文本 var data = { "PatientID": "XYX", "Email": "testing@gmail.com" }; $('#e6').select2('val', '123'); 但系统抛出以下错误:如果未定
var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
$('#e6').select2('val', '123');
但系统抛出以下错误:如果未定义initSelection(),则无法调用val()
即使我定义了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();
});
});
$(文档).ready(函数(){
$(“#e6”)。选择2({
占位符:“搜索电影”,
最小输入长度:1,
ajax:{//我们使用Select2的方便助手,而不是编写函数来执行请求
url:“data.aspx”,
数据类型:“json”,
安静百万:1000,
数据:功能(术语,第页){
返回{
姓名:任期
};
},
initSelection:函数(元素,回调){
变量数据={“PatientID”:“XYX”,“电子邮件”:testing@gmail.com" };
回调(数据);
},
结果:功能(数据){
var结果=[];
$。每个(数据、功能(索引、项目){
结果:推({
id:item['Email'],
正文:项目['PatientID']
});
});
返回{
结果:结果
};
},
},
});
});
window.onload=函数(){
变量数据={“PatientID”:“XYX”,“电子邮件”:testing@gmail.com" };
//当这被称为系统抛出错误
//回邮后,在select2文本框中显示值时需要此代码
$('e6')。选择2('val',数据);
}
$(文档).ready(函数(){
$(“#e6”)。在(“选择2选择”,功能(e){
//警报(“选择val=“+e.val+”choice=“+JSON.stringify(e.choice));
var id=document.getElementById(“”);
document.getElementById(“”).value=e.val;
id.value=e.val;
//导致回邮
id.单击();
});
});
您的脚本有错误。我也有同样的问题,我看到了你的问题。在我阅读了Select2的API文档之后,我意识到了我的错误
您应该将initSelection
放在与ajax
相同的级别。e、 g
$("#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
};
},
},
});
initSelection
不应位于ajax
-属性内。