Javascript 如果复选框被选中/取消选中,启用/禁用提交按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10021848/
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
Enable/Disable submit button if checkbox is checked/unchecked?
提问by HelpNeeder
How to enable the submit button in my HTML form and disable if checkbox was unchecked?
如果未选中复选框,如何启用 HTML 表单中的提交按钮并禁用?
What is wrong with this code?
这段代码有什么问题?
EnableSubmit = function(val)
{
var sbmt = document.getElementById("Accept");
if (val.checked == true)
{
sbmt.disabled = false;
}
else
{
sbmt.disabled = true;
}
}
The check box
复选框
<td width="30%">Do you accept Terms of Service agreement?</td>
<td width="10px" style="min-width: 10px"></td>
<td width="70%">
<input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit"> I agree to Terms of Service agreement.
</td>
回答by Paul Bruno
Change your HTML to this:
将您的 HTML 更改为:
<td width="30%">Do you accept Terms of Service agreement?</td>
<td width="10px" style="min-width: 10px"></td>
<td width="70%">
<input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit(this)"> I agree to Terms of Service agreement.
</td>
Reason it's not working: you're not passing your function anything. Actually, because you've not used parentheses with the function name, you've not called the function at all. Passing it this
in the context of the onclick
HTML event attribute means you're passing a reference to the element's DOM object, giving you access to its checked
property.
它不起作用的原因:你没有传递你的函数任何东西。实际上,因为您没有在函数名中使用括号,所以您根本没有调用该函数。this
在onclick
HTML 事件属性的上下文中传递它意味着您正在传递对元素的 DOM 对象的引用,从而使您可以访问其checked
属性。
回答by RyanS
You need to include the parameters: onClick="EnableSubmit()"
您需要包含以下参数: onClick="EnableSubmit()"
<input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit()"> I agree to Terms of Service agreement.
回答by shubhranshu
First if condition checks whether toggle is switched to desired side. Condition can be changed according to requirement. Last jquery is to make toggle button grey.
首先 if 条件检查切换是否切换到所需的一侧。条件可以根据需要改变。最后一个 jquery 是将切换按钮设置为灰色。
if(!$('#togglehk').is(":checked"))
{
$('#togglehk').click();
}
jQuery('#togglehk').attr("disabled", true);
jQuery('#togglehk').prop("disabled",true);
jQuery('#togglehk').next(".slider").css("backgroundcolor","#B3B3B3");