如何取消选中纯JavaScript中的复选框? [英] How to uncheck a checkbox in pure JavaScript?
问题描述
以下是HTML代码:
< div class =text>
< input value =truetype =checkboxchecked =name =copyNewAddrToBilling>< label>
我想将该值更改为false。或者只需取消选中该复选框。我希望在纯JavaScript中做到这一点,而不使用外部库(不包括jQuery等)。
< HTML>
< body>
< input id =check1type =checkboxchecked =name =copyNewAddrToBilling>
< / body>
< script language =javascript>
document.getElementById(check1)。checked = true;
document.getElementById(check1)。checked = false;
< / script>
< / html>
我已经将语言属性添加到了脚本元素中,但它并不是必需的,因为所有浏览器都将其用作一个默认的,但它毫无疑问是什么这个例子显示。
如果你想使用JavaScript来访问元素,它有一个非常有限的GetElement *函数集。所以你需要养成为每个元素赋予一个UNIQUE ID属性的习惯。
Here is the HTML Code:
<div class="text">
<input value="true" type="checkbox" checked="" name="copyNewAddrToBilling"><label>
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)
<html>
<body>
<input id="check1" type="checkbox" checked="" name="copyNewAddrToBilling">
</body>
<script language="javascript">
document.getElementById("check1").checked = true;
document.getElementById("check1").checked = false;
</script>
</html>
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中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!