Javascript 使用javascript取消选中页面加载上的所有复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12364007/
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
Uncheck all checkbox on pageload using javascript
提问by kkk
I am using following code but it is not working. Tell me if any suggestion. I want all chekboxes unchecked when I load the page. I've got the following code but it doesn't work:
我正在使用以下代码,但它不起作用。告诉我是否有任何建议。我希望在加载页面时取消选中所有复选框。我有以下代码,但它不起作用:
window.onload = function abc() {
document.getElementsByTagName('input')[0].focus();
}
<tr>
<td>
<input type="checkbox" ID="cb1" value="29500" onclick="if(this.checked){ cbcheck(this) } else { cbuncheck(this)}" /> Laptop
</td>
<td>
<a href="#" id="a1" onmouseover="showimage('a1','laptop1');" >Show Image</a>
<img src="Images/laptop.jpg" id="laptop1" alt="" style="display:none; width:150px; height:150px;" onmouseout="hideimage('a1','laptop1');" class="right"/>
</td>
</tr>
<tr>
<td>
<input type="checkbox" ID="cb2" value="10500" onclick="if(this.checked){ cbcheck(this) } else { cbuncheck(this)}" /> Mobile
</td>
<td>
<a href="#" id="a2" onmouseover="showimage('a2','mobile1');" >Show Image</a>
<img src="Images/mobile.jpg" id="mobile1" alt="" style="display:none; width:150px; height:150px;" onmouseout="hideimage('a2','mobile1');" />
</td>
</tr>
回答by Talha
Call this function on your page load event
在页面加载事件上调用此函数
function UncheckAll(){
var w = document.getElementsByTagName('input');
for(var i = 0; i < w.length; i++){
if(w[i].type=='checkbox'){
w[i].checked = false;
}
}
}
回答by rationalboss
I don't see your code attempting to uncheck the boxes. You are only trying to focus on an element.
我没有看到您的代码试图取消选中这些框。你只是想专注于一个元素。
window.onload = function abc() {
document.getElementsByTagName('input')[0].focus();
var a = document.getElementById('form_name').getElementsByTagName('input');
for (var i=0;i<a.length;i++) {
if (a[i].type == 'checkbox') a[i].checked = false;
}
}
I also advise that you do try out JQuery. The above code would be just like this in JQuery:
我还建议您尝试使用JQuery。上面的代码在 JQuery 中是这样的:
$(document).ready(function(){
$('#formID input[type=checkbox]').attr('checked',false);
});
回答by Yograj Gupta
You should try
你应该试试
window.onload = function(){
var checkboxes = document.getElementsByTagName("INPUT");
for(var x=0; x<checkboxes.length; x++)
{
if(checkboxes[x].type == "checkbox")
{
checkboxes[x].checked = false;
}
}
}
and If you can use jQuery, you can try
如果你可以使用 jQuery,你可以尝试
$(function(){
$('input[type=checkbox]').prop("checked", false);
});
回答by a guest
A new answer to showcase new technology. Vanilla JS now in 2015:
展示新技术的新答案。Vanilla JS 现在在 2015 年:
var list = document.querySelectorAll('input[type=checkbox]');
for (var item of list) {
item.checked = false;
}
Compact one-line variation:
紧凑的单线变体:
for(var i of document.querySelectorAll('[type=checkbox]')) { i.checked = false; }
This is straight out of NodeList MDN documentationexamples. The list provided by querySelectorAll is a NodeList
and the for...of
loop is a new statement for iterating over property values, a part of 2015 ECMAScript 6 standard—See herefor browser compatibility.
这直接来自NodeList MDN 文档示例。querySelectorAll 提供的列表是一个NodeList
,for...of
循环是一个用于迭代属性值的新语句,它是 2015 ECMAScript 6 标准的一部分——浏览器兼容性见这里。