我的嵌套 for 循环遇到了一些问题。我正在尝试确定表单中选中复选框的值。有人告诉我,整个事情的格式应该是这样的:
for(checkboxes loop){
for(item.appointmentType[1] loop){
if(statement conditional){
set checks code
}
}
}
但我似乎无法正确编写它,因为我收到一条错误消息,说 Uncaught TypeError: Object has no method 'setAttribute'
下面是我正在使用的代码。有人可以帮我纠正它可能知道我在说什么吗?
var checkboxes = document.forms[0].appointmentType;
for(var i=0; i<checkboxes.length; i++){
for(var j=0; j<item.appointmentType[1].length; j++){
if(checkboxes[i].value === item.appointmentType[1][j]){
item.appointmentType[1][j].setAttribute("checked", "checked");
}
}
}
这是与 javascript 一起使用的 HTML 代码。所有的 javascript 都包含在它自己的 .js 文件中。
<ul><span class="lab">
<li><input type="checkbox" checked value="Get A Free Estimate" id="Get A Free Estimate"
name="appointmentType" /><label for="getAFreeEstimate"><span class="gfe">Get A FREE
Estimate</span></label></li>
<li><input type="checkbox" value=" Lawn Service" id="lawnService" name="appointmentType"/
<label for="lawnService"><span class="ls">Lawn Service</span></label></li>
<li><input type="checkbox" value=" Sprinkler System Service" id="sprinklerSystemService"
name="appointmentType" /><label for="sprinklerSystemService"><span
class="sss">Sprinkler System Service</span></label></li>
<li><input type="checkbox" value=" Flower Bed Maintenance" id="flowerBedMaintenance"
name="appointmentType" /><label for="flowerBedMaintenance"><span class="fbm">Flower
Bed Maintenance</span></label></li>
<li><input type="checkbox" value=" Hedge/Tree Service" id="hedgeTreeService"
name="appointmentType" /><labe for="hedgeTreeService"><span class="hts">Hedge / Tree
Service</span></label></li>
<li><input type="checkbox" value=" Handyman Services" id="handymanServices"
name="appointmentType" /><label for="handymanServices"><span class="hs">Handyman
Services</span></label></li>
<li><input type="checkbox" value=" Other" id="other" name="appointmentType" /><label
for="other"><span class="otr">Other</span></label></li></span></ul><br/>