2012-04-05 94 views
11

如何在我的HTML表单中启用提交按钮并禁用复选框未选中状态?如果复选框被选中/取消选中,启用/禁用提交按钮?

这段代码有什么问题?

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

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

复选框

<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> 
+0

的可能重复[如何禁用提交按钮时复选框取消选中?(http://stackoverflow.com/questions/5458531/how- do-i-disable-a-submit-button-when-checkbox-is-uncheck) – 2014-07-06 10:59:53

回答

12

更改您的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> 

原因,它不工作:你不通过你的函数的任何信息。实际上,因为你没有在函数名称中使用圆括号,所以你根本没有调用函数。在onclick HTML事件属性的上下文中传递this意味着您传递对元素的DOM对象的引用,从而允许您访问其checked属性。

+2

您是否可以重新输入您的问题以包含表单(HTML)的其余源代码?还有其他可能使用的JavaScript? – 2012-04-05 05:41:25

3

您需要包括的参数:onClick="EnableSubmit()"

<input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit()"> I agree to Terms of Service agreement. 
相关问题