php Javascript选中所有并取消选中所有复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17483771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript check all and uncheck all for checkbox
提问by Vignesh Pichamani
<script type="text/javascript">
function checkAll(formname, checktoggle)
{
var checkboxes = new Array();
checkboxes = document[formname].getElementsByTagName('input');
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type === 'checkbox') {
checkboxes[i].checked = checktoggle;
}
}
}
</script>
<form name="myform">
<li>
<label class="cba">
<a href="javascript:void();" onclick="javascript:checkAll('myform', true);">Check All</a> |
<a href="javascript:void();" onclick="javascript:checkAll('myform', false);">UnCheck All</a>
</label>
</li>
<li>
<input class="cba" type="checkbox" name="content1" value="1"<?php checked('1', $slct); ?>/>
</li>
<li>
<input class="cbc" type="checkbox" name="content2" value="2"<?php checked('2', $copy); ?>/>
</li>
<li>
<input class="cbx" type="checkbox" name="content3" value="3"<?php checked('3', $cut); ?>/>
</li>
</form>
Hi all I have made the toggle option for the checkbox check all and uncheck all. Still now Check all and uncheck all is not working I get the error in console while viewing in firebug. Here is the screenshot i attached. I am not sure what i did mistake.
大家好,我已将复选框的切换选项选中并取消选中。现在仍然全选并取消全选不起作用我在萤火虫中查看时在控制台中收到错误。这是我附上的截图。我不确定我做错了什么。
Any Suggestion would be great.
任何建议都会很棒。
Thanks, vicky
谢谢,维琪
回答by Mark Walters
Your syntax is incorrect. Your missing the .forms
so it should look like this
你的语法不正确。你错过了.forms
所以它应该是这样的
document.forms[formName].getElementsByTagName("input");
回答by Vignesh Pichamani
function checkAll(formname, checktoggle)
{
var checkboxes = new Array();
checkboxes = document.forms[formname].getElementsByTagName('input');
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type === 'checkbox') {
checkboxes[i].checked = checktoggle;
}
}
}
</script>
Finally based on @Mark Walters Suggestion I Correct the problem. Here is the One I changed based on his suggestion. Thanks for all your Help. Happy Day
最后基于@Mark Walters 的建议我更正了这个问题。这是我根据他的建议更改的那个。感谢你的帮助。愉快的一天
回答by Prem Kumar Maurya
Javascript function to toggle (check/uncheck) all checkbox.
用于切换(选中/取消选中)所有复选框的 Javascript 函数。
function checkAll(bx)
{
var cbs = document.getElementsByTagName('input');
for(var i=0; i < cbs.length; i++)
{
if(cbs[i].type == 'checkbox')
{
cbs[i].checked = bx.checked;
}
}
}
回答by Black Sea
function checkAll(bx) {
var cbs = document.getElementsByTagName('input');
for(var i=0; i < cbs.length; i++) {
if(cbs[i].type == 'checkbox') {
cbs[i].checked = bx.checked;
}
}
}
Have that function be called from the onclick attribute of your checkbox to check all.
从复选框的 onclick 属性调用该函数以检查所有内容。
<input type="checkbox" onclick="checkAll(this)">
回答by golempremier
Try this:
尝试这个:
function checkAll(formname, checktoggle)
{
var checkboxes = new Array();
checkboxes = document.form.myform.getElementsByTagName('input');
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type === 'checkbox') {
checkboxes[i].checked = checktoggle;
}
}
}
回答by Eolia
For specific checkboxes, you can use this:
对于特定的复选框,您可以使用:
function checkBoxes(pForm, boxName) {
for (i = 0; i < pForm.elements.length; i++)
if (pForm.elements[i].name == boxName)
pForm.elements[i].checked = pForm.elements[i].checked ? false : true;
}
}