我已将此脚本用于 jquery 多选,在这我得到值基于它应该签入 jquery 数组中的值的数组中多选。
asp 下拉列表 id:ddlDepartment
hdnDepartment在此隐藏的字段会被选中的所有值
$(document).ready(function () {
var revalue = new Array();
if (document.getElementById("<%=hdnDepartment.ClientID %>").value != "") {
var str = document.getElementById("<%=hdnDepartment.ClientID %>").value;
var obj = $("#<%=ddlDepartment.ClientID %>");
alert(obj);
revalue = str.split(',');
var i;
for (i = 0; i < revalue.length; i++) {
//should reload that values in the checked in jquery multiselect
}
}
});
把 hdnDepartment 放在周围的 div
<div id="departmentCheck">
<input type="hidden" value="true,true,false,false" id="hdnDepartment" />
</div>
jquery 应该是
$(document).ready(function () {
var revalue = new Array();
if ($('#departmentCheck input').val() != "") {
var str = $('#departmentCheck input').val();
revalue = str.split(',');
var counter=0;
$('#idOfMultiselect option').each(function(k,v){
try{
//if catch if there is no index at counter in revalue then default to false
$(v).attr('checked', revalue[counter]);
}
catch{
$(v).attr('checked',false);
}
counter++;
});
}
});