How to set 2 items are default selected in multiselect
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 {
开发者_Go百科 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);
}
});
From the doc of the plugin.
Manually check or uncheck a checkbox?
The checkboxes can be accessed after calling the "widget" method. Simply manually trigger the NATIVE click event on them:
// manually check (or uncheck) the third checkbox in the menu: $("select").multiselect("widget").find(":checkbox").each(function(){ this.click(); });
The native click event must be used (trigger('click') will not work) due to this bug in jQuery’s core.
All necessary events and actions, like updating the button value, will automatically fire.
Alternatively, you could give the original option tag the selected attribute, and then call MultiSelect’s refresh method.
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'
like control.val(a_list_selected[i]).attr('selected','selected');
(see http://reference.sitepoint.com/html/option/selected for example for the value it could take)
精彩评论