最开始获取的是input value值设置checkbox选中状态,由于input value要使用计算业务,后来改造为id
<input type="checkbox" name="ChekRole" value="1" >超级管理员
<input type="checkbox" name="ChekRole" value="2">学生
<input type="checkbox" name="ChekRole" value="3">教师
<script type="text/javascript">
var name = "1,2,3";
var names = name.split(",");
var inputs = document.getElementsByTagName('input');
for (var i =0;i<inputs.length;i++) {
for (var n =0;n<names.length;n++) {
if (names[n]===inputs[i].value) {
inputs[i].checked = true;
}
}
}
</script>
jQuery
$(name.split(",")).each(function (i,e){
$("input[name='ChekRole'][value='"+e+"']").prop("checked",true);
<input type="checkbox" id="test1" name="ChekRole" value="1" >超级管理员
<input type="checkbox" id="test2" name="ChekRole" value="2">学生
<input type="checkbox" id="test3" name="ChekRole" value="3">教师
<script type="text/javascript">
document.oncontextmenu = function(){return false;}
var name = "test1,test2"
var names = name.split(",")
var inputs = document.getElementsByTagName('input')
for (var i =0;i<inputs.length;i++) {
for (var n =0;n<names.length;n++) {
if (names[n]===inputs[i].id) {
inputs[i].checked = true;
}
}
}
</script>