验证asp.net中的单个复选框 [英] Validation of single checkbox in asp.net
问题描述
我有一个表单,除非用户勾选了条款和条件复选框(chkTerms
),否则不应该提交该表单.取消提交表单,防止提交表单的最佳方法是什么?
I have a form that should not be submitted until the user has ticked the terms and conditions checkbox (chkTerms
). What is the best way to prevent submission of the form if it is unticked?
本来可以在form标签中调用JavaScript函数,但是由于各种原因,它已嵌入到我的母版页中,因此我认为这是不可行的.例如可以在jQuery中完成吗? 谢谢
I would have put a call to a JavaScript function in the form tag but for various reasons this is embedded in my master page so I assume this is a no go. Can this be done in jQuery for instance? Thanks
推荐答案
在表单加载时禁用提交按钮. 仅在选中复选框时启用它,然后在取消选中复选框时再次禁用它.
Disable your submit button on form load. Only enable it when checkbox is ticked and again disable it on uncheck of checkbox.
检查以下示例:
<input type="submit" id="btn" disabled="true" value="Submit"/>
<input type="checkbox" id="chk" onchange="submitStatus();">Terms and Conditions</input>
您的脚本功能应该是
function submitStatus(){
if(document.getElementById('chk').checked){
document.getElementById('btn').disabled = false;
}else{
document.getElementById('btn').disabled = true;
}
}
这篇关于验证asp.net中的单个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!