Here is the HTML Code:
这里是HTML代码:
I want to change the value to false. Or just uncheck the checkbox. I'd like to do this in pure Javascript, without the use of external libraries (no jQuery etc) 我想把这个值改为false。或者取消复选框。我想用纯Javascript,不使用外部库(没有jQuery等) 4 个解决方案 #1 57 I have added the language attribute to the script element, but it is unnecessary because all browsers use this as a default, but it leaves no doubt what this example is showing. 我已经将语言属性添加到脚本元素中,但这是不必要的,因为所有浏览器都将其作为默认属性,但这无疑说明了这个示例所显示的内容。 If you want to use Javascript to access elements, it has a very limited set of GetElement* functions. So you are going to need to get into the habit of giving every element a UNIQUE id attribute. 如果您想使用Javascript访问元素,它有一组非常有限的GetElement*函数。所以你需要养成给每个元素一个唯一id属性的习惯。 #2 7 Recommended, without jQuery: 推荐,没有jQuery: Give your an ID and refer to that. Also, remove the checked="" part of the tag if you want the checkbox to start out unticked. Then it's: 给你的一个ID并引用它。此外,如果希望复选框开始时不勾选,则删除标记的勾选=""部分。然后是: document.getElementById("my-checkbox").checked = true; Pure Javascript, with no Element ID (#1): 纯Javascript,无元素ID (#1): var inputs = document.getElementsByTagName('input'); for(var i = 0; i Pure Javascript, with no Element ID (#2): 纯Javascript,无元素ID (#2): document.querySelectorAll('.text input[name="copyNewAddrToBilling"]')[0].checked = true; document.querySelector('.text input[name="copyNewAddrToBilling"]').checked = true; Note that the querySelectorAll and querySelector methods are supported in these browsers: IE8+, Chrome 4+, Safari 3.1+, Firefox 3.5+ and all mobile browsers. 注意,这些浏览器支持querySelectorAll和querySelector方法:IE8+、Chrome 4+、Safari 3.1+、Firefox 3.5+和所有移动浏览器。 If the element may be missing, you should test for its existence, e.g.: 如果元素可能丢失,您应该测试它的存在,例如: var input = document.querySelector('.text input[name="copyNewAddrToBilling"]'); if (!input) { return; } With jQuery: jQuery: $('.text input[name="copyNewAddrToBilling"]').prop('checked', true); #3 5 There is another way to do this: 还有另外一种方法: //elem - get the checkbox element and then elem.setAttribute('checked', 'checked'); //check elem.removeAttribute('checked'); //uncheck #4 0 You will need to assign an ID to the checkbox: 您需要为复选框分配一个ID: and then in Javascript: 然后在Javascript中: document.getElementById("checkboxId").checked = false;
I want to change the value to false. Or just uncheck the checkbox. I'd like to do this in pure Javascript, without the use of external libraries (no jQuery etc)
我想把这个值改为false。或者取消复选框。我想用纯Javascript,不使用外部库(没有jQuery等)
57
I have added the language attribute to the script element, but it is unnecessary because all browsers use this as a default, but it leaves no doubt what this example is showing.
我已经将语言属性添加到脚本元素中,但这是不必要的,因为所有浏览器都将其作为默认属性,但这无疑说明了这个示例所显示的内容。
If you want to use Javascript to access elements, it has a very limited set of GetElement* functions. So you are going to need to get into the habit of giving every element a UNIQUE id attribute.
如果您想使用Javascript访问元素,它有一组非常有限的GetElement*函数。所以你需要养成给每个元素一个唯一id属性的习惯。
7
Recommended, without jQuery:
推荐,没有jQuery:
Give your an ID and refer to that. Also, remove the checked="" part of the tag if you want the checkbox to start out unticked. Then it's:
checked=""
给你的一个ID并引用它。此外,如果希望复选框开始时不勾选,则删除标记的勾选=""部分。然后是:
document.getElementById("my-checkbox").checked = true;
Pure Javascript, with no Element ID (#1):
纯Javascript,无元素ID (#1):
var inputs = document.getElementsByTagName('input'); for(var i = 0; i
Pure Javascript, with no Element ID (#2):
纯Javascript,无元素ID (#2):
document.querySelectorAll('.text input[name="copyNewAddrToBilling"]')[0].checked = true; document.querySelector('.text input[name="copyNewAddrToBilling"]').checked = true;
Note that the querySelectorAll and querySelector methods are supported in these browsers: IE8+, Chrome 4+, Safari 3.1+, Firefox 3.5+ and all mobile browsers.
querySelectorAll
querySelector
注意,这些浏览器支持querySelectorAll和querySelector方法:IE8+、Chrome 4+、Safari 3.1+、Firefox 3.5+和所有移动浏览器。
If the element may be missing, you should test for its existence, e.g.:
如果元素可能丢失,您应该测试它的存在,例如:
var input = document.querySelector('.text input[name="copyNewAddrToBilling"]'); if (!input) { return; }
With jQuery:
jQuery:
$('.text input[name="copyNewAddrToBilling"]').prop('checked', true);
5
There is another way to do this:
还有另外一种方法:
//elem - get the checkbox element and then elem.setAttribute('checked', 'checked'); //check elem.removeAttribute('checked'); //uncheck
0
You will need to assign an ID to the checkbox:
您需要为复选框分配一个ID:
and then in Javascript:
然后在Javascript中:
document.getElementById("checkboxId").checked = false;