本文介绍了根据数组中的值检查复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用数组或值检查复选框
HTML代码:
< div id =weekdays>
< label for =星期六>周六< / label>< input type ='checkbox'name ='week_day []'value ='saturday'id =saturday>
< label for =sunday>周日< / label>< input type ='checkbox'name ='week_day []'value ='sunday'id =sunday>
< label for =星期一>周一< / label>< input type ='checkbox'name ='week_day []'value ='monday'id =monday>
< label for =tuesday>星期二< / label>< input type ='checkbox'name ='week_day []'value ='tuesday'id =tuesday>
< label for =wednesday>星期三< / label>< input type ='checkbox'name ='week_day []'value ='wednesday'id =wednesday>
< label for =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>
strong>
var f = [星期六,星期日,星期一]
$ c
$ b我想检查星期六,星期日和星期一这个表格解决方案使用数组的forEach方法,即使没有JQuery也是非常简单的。 $ p $ f.forEach(function(i){
document.getElementById(i).checked = true;
});
您可以在
Checkbox check with Array or values
Html Code:
<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">
Array
var f = ["saturday", "sunday", "monday"]
infoI wanna check the days saturday, sunday and monday on this form
解决方案
With the forEach method of Array this is very simple even without JQuery.
f.forEach(function(i){
document.getElementById(i).checked = true;
});
You can read more about for each on the Mozilla Developer Network
这篇关于根据数组中的值检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!