Html代码:
<div id="weekdays">
<label for="saturday"> Saturday </label><input type='checkbox' name='week_day[]' value='saturday' id="saturday">
<label for="sunday"> Sunday </label><input type='checkbox' name='week_day[]' value='sunday' id="sunday">
<label for="monday"> Monday </label><input type='checkbox' name='week_day[]' value='monday' id="monday">
<label for="tuesday"> Tuesday </label><input type='checkbox' name='week_day[]' value='tuesday' id="tuesday">
<label for="wednesday"> Wednesday </label><input type='checkbox' name='week_day[]' value='wednesday' id="wednesday">
<label for="thursday"> Thursday </label><input type='checkbox' name='week_day[]' value='thursday' id="thursday">
<label for="friday"> Friday </label><input type='checkbox' name='week_day[]' value='friday' id="friday">
阵列
var f = ["saturday", "sunday", "monday"]
信息 我想查看星期六,星期日和星期一这个表格上的日子
答案 0 :(得分:6)
答案 1 :(得分:1)
一种简单的方法是迭代数组并使用prop
方法和id选择器。
for(var i=0;i<f.length;i++) {
$('#' + f[i]).prop('checked', true)
}
<强> Check Fiddle 强>
答案 2 :(得分:1)
使用Array的forEach方法,即使没有JQuery,这也非常简单。
f.forEach(function(i){
document.getElementById(i).checked = true;
});
上详细了解每个内容