如何取消选中纯 JavaScript 中的复选框? [英] How to uncheck a checkbox in pure JavaScript?

查看:10
本文介绍了如何取消选中纯 JavaScript 中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 HTML 代码:

<input value="true" type="checkbox" checked="" name="copyNewAddrToBilling"><label>

我想将该值更改为 false.或者只是取消选中复选框.我想在纯 JavaScript 中执行此操作,而不使用外部库(没有 jQuery 等)

解决方案

<身体><input id="check1" type="checkbox" checked="" name="copyNewAddrToBilling"><脚本语言="javascript">document.getElementById("check1").checked = true;document.getElementById("check1").checked = false;

我已经在 script 元素中添加了语言属性,但这是不必要的,因为所有浏览器都使用它作为默认值,但毫无疑问,这个例子显示的是什么.

如果您想使用 javascript 来访问元素,它的 GetElement* 函数集非常有限.所以你需要养成给每个元素一个唯一的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆