如果复选框被选中/取消选中,启用/禁用提交按钮? [英] Enable/Disable submit button if checkbox is checked/unchecked?

查看:147
本文介绍了如果复选框被选中/取消选中,启用/禁用提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这段代码有什么问题?

如何在我的HTML表单中启用提交按钮并禁用复选框?

  EnableSubmit = function(val)
{
var sbmt = document.getElementById(Accept);

if(val.checked == true)
{
sbmt.disabled = false;
}
else
{
sbmt.disabled = true;




$ b $复选框

 < td width =30%>您接受服务条款协议吗?< / td> 
< td width =10pxstyle =min-width:10px>< / td>
< td width =70%>
< input type =checkboxname =TOSvalue =AcceptonClick =EnableSubmit>我同意服务条款协议。
< / td>


解决方案

将您的HTML更改为:

 < td width =30%>您接受服务条款协议吗?< / td> 
< td width =10pxstyle =min-width:10px>< / td>
< td width =70%>
< input type =checkboxname =TOSvalue =AcceptonClick =EnableSubmit(this)>我同意服务条款协议。
< / td>

原因不起作用:您没有任何功能。实际上,因为你没有在函数名称中使用圆括号,所以你根本没有调用函数。在 onclick HTML事件属性的上下文中传递这个意味着你传递了对元素的DOM对象的引用,让你访问它的选中的属性。


How to enable the submit button in my HTML form and disable if checkbox was unchecked?

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>

解决方案

Change your HTML to this:

<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.

这篇关于如果复选框被选中/取消选中,启用/禁用提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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