如何设置多选时默认选择2个项目
我的下面的代码使用项目值 1 和 2 填充多选,默认选择,但仅选择项目值 2:
$.ajax({
type: "POST",
url: Url_function,
data: para_datas,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnPopulateControl(response) {
list = response.d;
control.multiselect({
selectedList: selectedlist,
header: false,// 0-based index
selectedList: 2
});
var $select = control.multiselect('disable');
if (list.length > 0) {
$select.multiselect('enable');
$.each(list, function () {
control.append($("<option></option>").val(this['Value']).html(this['Text']));
//console.log(this['Text']);
});
}
else {
control.empty().append('<option selected="selected" value="0">Not available<option>');
}
a_list_selected = valueselected.split(',');//valueselected = '1,2'
for (i =0; i< a_list_selected.length; i++)
{
control.val(a_list_selected[i]).attr('selected',true);
}
control.multiselect('refresh'); //refresh the select here
},
error: function () {
alert(Error_message);
}
});
My code below to populate multiselect with item value 1 and 2 are default selected but only item value 2 is selected :
$.ajax({
type: "POST",
url: Url_function,
data: para_datas,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnPopulateControl(response) {
list = response.d;
control.multiselect({
selectedList: selectedlist,
header: false,// 0-based index
selectedList: 2
});
var $select = control.multiselect('disable');
if (list.length > 0) {
$select.multiselect('enable');
$.each(list, function () {
control.append($("<option></option>").val(this['Value']).html(this['Text']));
//console.log(this['Text']);
});
}
else {
control.empty().append('<option selected="selected" value="0">Not available<option>');
}
a_list_selected = valueselected.split(',');//valueselected = '1,2'
for (i =0; i< a_list_selected.length; i++)
{
control.val(a_list_selected[i]).attr('selected',true);
}
control.multiselect('refresh'); //refresh the select here
},
error: function () {
alert(Error_message);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自插件的文档。
您已选择了选定的方式,但在代码中,您的设置选择为 true
control.val(a_list_selected[i]).attr('selected',true);
。设置它的值是'selected'
就像control.val(a_list_selected[i]).attr('selected','selected');
(参见 http://reference.sitepoint.com/html/option/selected 的值拿)From the doc of the plugin.
You've opted for the selected way but in your code, your setting selected to true
control.val(a_list_selected[i]).attr('selected',true);
. The value to set it is'selected'
likecontrol.val(a_list_selected[i]).attr('selected','selected');
(see http://reference.sitepoint.com/html/option/selected for example for the value it could take)