仅在未选中复选框时调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11800257/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:08:13  来源:igfitidea点击:

Call javascript function only when checkbox is NOT checked

javascriptjavascript-eventscheckboxcall

提问by Jeff Prachyl

I'm trying to figure out how to call a javascript function only when a checkbox is NOT checked.

我试图弄清楚如何仅在未选中复选框时调用 javascript 函数。

here is my checkbox:

这是我的复选框:

<input type="checkbox" id="icd" name="icd" value="icd" />

here is the name of the function:

这是函数的名称:

planhide();

Thanks!

谢谢!

回答by 0x499602D2

document.getElementById('icd').onchange = function() {
    if ( document.getElementById('icd').checked === false ) {
        planhide();
    }
};?

回答by vivek_jonam

Include onchange option in the input tag and then add an intermediate function that checks and calls planhide() accordingly as follows:

在 input 标签中包含 onchange 选项,然后添加一个中间函数来相应地检查和调用 planhide() ,如下所示:

<input type="checkbox" id="icd" name="icd" value="icd" onchange=check()/>

Then define the check() to do check the state and call the function as follows:

然后定义 check() 来检查状态并调用函数,如下所示:

function check()
{
if(document.getElementById("icd").checked==false)
planhide();
}

Also instead of onchange you can also use onclick on the submit button option to call the check() function as like follows:

此外,您还可以在提交按钮选项上使用 onclick 来调用 check() 函数,而不是 onchange,如下所示:

<input type="button" onclick=check()/>

回答by user3642940

$(document).ready(function () {
   $('#icd').change(function () {
      if (!this.checked) {
          planhide();
    }
   });
});

回答by hvgotcodes

just register an onchangehandler on your input, check the 'checked' property when the handler is call, and call the method if checked is false.

只需onchange在您的输入上注册一个处理程序,在调用处理程序时检查“已检查”属性,如果检查为假,则调用该方法。

Here is a fiddle.

这是一个小提琴。