Javascript 如何使用javascript检查复选框是选中还是未选中?

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

How to check if a checkbox is checked or unchecked with javascript?

javascript

提问by Nanek

I am trying to check weather a checkbox is checked or unchecked. This is my code:

我试图检查天气复选框被选中或取消选中。这是我的代码:

<script type="text/javascript">
function EnableDisableToolTip() {
    if (document.forms[0].help_text.checked) {
        alert("Checked");
    }
    else if (!document.forms[0].help_text.checked) {
    alert("Unchecked");
    }        
}
</script>


<div id="tooltiponoff">
<form action="">
@Html.CheckBox("help_text", true, new { id = "help_text", onclick = "EnableDisableToolTip()" })Hj?lpetekst
</form>
</div>

It only alerts Unchecked when i click it

当我点击它时它只提醒未选中

Thanks in advance

提前致谢

采纳答案by chrisf

It seems to work for me - please see this fiddle. Note that the event listener is added by Javascript, instead of using the inline onclicksyntax.

它似乎对我有用- 请看这个小提琴。请注意,事件侦听器是由 Javascript 添加的,而不是使用内联onclick语法。

回答by Ovais Khatri

try

尝试

< script type="text/javascript" >
function EnableDisableToolTip(Control) {
    if (Control.checked) {
        alert("Checked");
    }
    else {
    alert("Unchecked");
    }        
}
< /script >


< div id="tooltiponoff" >
< form action="" >
@Html.CheckBox("help_text", true, new { id = "help_text", onclick = "EnableDisableToolTip(this)" })Hj?lpetekst
< /form >
< /div >

回答by tjg184

Make sure you are correctly referencing the object by using the console or alerting document.forms[0].help_text. It's very likely it's not the right reference.

确保您通过使用控制台或警报 document.forms[0].help_text 正确引用对象。这很可能不是正确的参考。

alert(document.forms[0].help_text);

回答by Hasan Tuna Oru?

Your code is very well but not greatable. For example when many click the checkbox after 1 unclick then checkbox is start over. My English so bad i cannot explain But: You check many checkbox and then fall the if statement. And then unclick one, this is falling else statement. And checked ones falling down. Your code is developing like this:

你的代码很好,但不是很好。例如,当许多人在 1 次取消单击后单击复选框时,复选框将重新开始。我的英语太糟糕了,我无法解释但是:您选中了许多复选框,然后在 if 语句中落下。然后取消单击一个,这是下降的 else 语句。并检查掉下来的。你的代码是这样开发的:

            var count = 0;
            function Tik(Control) {
                if (Control.checked) {
                    count++;
                }
                else {
                    count--;
                }
                if (count > 0) {
                    document.getElementById("btnMessageDel").disabled = false;
                }
                else {
                    document.getElementById("btnMessageDel").disabled = true;
                }
            }

<input type="submit" id="btnMessageDel" name="btnMessageDel" class="btn btn-primary" value="Delete" form="messagesform" disabled />
<input type="checkbox" value="@MessageId" name="chkMessage" id="chkMessage" onclick="Tik(this)" />