admin管理员组文章数量:1279176
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
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
Share Improve this question asked Jun 28, 2011 at 11:55 NanekNanek 3534 gold badges9 silver badges16 bronze badges 2- 3 possible duplicate of Javascript to check whether a checkbox is being checked or unchecked – Dominic Rodger Commented Jun 28, 2011 at 11:56
-
check what
alert(document.forms[0].help_text)
gives.. ? May be if undefined, it would always go in else condition.. – niksvp Commented Jun 28, 2011 at 11:58
4 Answers
Reset to default 2It seems to work for me - please see this fiddle. Note that the event listener is added by Javascript, instead of using the inline onclick
syntax.
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 >
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.
alert(document.forms[0].help_text);
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:
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)" />
本文标签: How to check if a checkbox is checked or unchecked with javascriptStack Overflow
版权声明:本文标题:How to check if a checkbox is checked or unchecked with javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741278779a2369886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论