使用Javascript检查并取消选中复选框 [英] Check and un-check checkboxes using Javascript
问题描述
嘿,我想尝试得到一个复选框的情况下,我有7个盒子,我想获得一个逻辑语句,它的工作原理。我有7个复选框,第7个框是以上所有,当上述所有点击它取消选择所有以前的,当选择1-6时,它取消选择上面的所有框。我目前的代码发生了什么,它取消选择所有的1-6框,然后他们现在无法点击。不幸的是我有点限制。所以我将粘贴我的代码任何帮助非常感激。
Hey guys im trying to get a checkbox scenario worked out, I have 7 boxes and im trying to get a logic statement where it works things out. I have 7 checkboxes and the 7th box is all of the above, when all of the above is clicked it deselects all of the previous ones, when 1-6 is selected it deselects the all of the above box. What ends up happening in my current code it deselects all of the 1-6 boxes and then they are now unable to click. Unfortunately i'm kind of constrained to things. so i'll paste my code any help greatly appreciated.
这是一个非常可怕的编码的代码段,我只是通过这一起,而我试图多种方式工作。
This is a snippet of very horrible coding, i just through this together while i was trying multiple ways to get it to work.
if (document.forms[0].propDetails[6].checked==true) {
for (var x=0;x<6;x++) {
document.forms[0].propDetails[x].checked=false;
}
}
else {
document.forms[0].propDetails[6].checked=false;
}
} // end of function
推荐答案
我首先建议您为1-6个复选框指定一个特定的NAME属性,并使用getElementsByName解析它们:
I first suggest that you give a specific NAME attribute to the 1-6 checkboxes, and parsing them using getElementsByName like so :
<input type="checkbox" id="myChk1" name="myChk" />
...
<input type="checkbox" id="myChk6" name="myChk" />
<input type="checkbox" id="myChkAll" onchange="chkAll(this);" />
<script type="text/javascript">
function chkAll(obj) {
var isChecked = obj.checked;
var chk1to6 = document.getElementsByName('myChk');
for (var i = 0 ; i < chk1to6.length ; i++) {
chk1to6[i].checked = isChecked;
}
}
</script>
这篇关于使用Javascript检查并取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!