使用javascript切换复选框 [英] toggle checkbox with javascript

查看:41
本文介绍了使用javascript切换复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要取消选中使用javascript的复选框.我有一个按钮,只需取消选中该框即可正常工作:

I want to uncheck a checkbox using javascript. I have one button which just unchecks the box and works fine:

function clear() {
document.getElementById("check").checked = "";
}

我还有另一个按钮,如果未选中,我想选中该框,然后取消选中它.下面不会取消选中该框.我可以切换if语句,其工作方式却相反.有什么问题吗?

I have another button that I want to check the box if not checked and uncheck if it is. Below doesn't uncheck the box. I can can switch the if statement and does works the opposite way. What's the problem?

function switchIt() {
if (document.getElementById("check").checked !== "checked") {
  document.getElementById("check").checked = "checked";
} else {
  document.getElementById("check").checked = "";
}

谢谢!

推荐答案

开关

function switchIt() {
  var box = document.getElementById("check");
  
  if (box.checked) {
    box.checked = false;
  } else {
    box.checked = true;
  }
}

setInterval(switchIt, 1000);

<input type="checkbox" id="check" />

这篇关于使用javascript切换复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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