[英]Enable/Disable submit button if checkbox is checked/unchecked?
如何在我的 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>
將您的 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>
它不起作用的原因:你沒有傳遞你的 function 任何東西。 實際上,因為您沒有在名稱 function 中使用括號,所以您根本沒有調用 function。 在onclick
HTML 事件屬性的上下文中傳遞this
意味着您正在傳遞對元素的 DOM object 的引用,從而使您可以訪問其checked
屬性。
您需要包含參數: onClick="EnableSubmit()"
<input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit()"> I agree to Terms of Service agreement.
首先 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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.