2012-04-05 95 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. 
相關問題