Javascript asp.net中单个复选框的验证
我有一个表单,在用户勾选“条款和条件”复选框(Javascript asp.net中单个复选框的验证,javascript,asp.net,jquery,Javascript,Asp.net,Jquery,我有一个表单,在用户勾选“条款和条件”复选框(chkTerms)之前不应提交该表单。如果表格未勾选,防止提交的最佳方法是什么 我本来会在表单标签中调用JavaScript函数,但由于各种原因,它嵌入在我的母版页中,所以我认为这是不可能的。例如,这可以在jQuery中完成吗? 谢谢是的,在jQuery中这很简单: $(document).ready(function(){ $('#myform').submit(function(e){ // when the form is loaded
chkTerms
)之前不应提交该表单。如果表格未勾选,防止提交的最佳方法是什么
我本来会在表单标签中调用JavaScript函数,但由于各种原因,它嵌入在我的母版页中,所以我认为这是不可能的。例如,这可以在jQuery中完成吗?
谢谢是的,在jQuery中这很简单:
$(document).ready(function(){
$('#myform').submit(function(e){ // when the form is loaded
if (!$('input[name="chkTerms"]')[0].checked) { // if the input with the name "chkTerms" is not checked
e.preventDefault(); // prevent the form submission
// provide feedback to the user in some way
}
}
});
是的,在jQuery中,这非常简单:
$(document).ready(function(){
$('#myform').submit(function(e){ // when the form is loaded
if (!$('input[name="chkTerms"]')[0].checked) { // if the input with the name "chkTerms" is not checked
e.preventDefault(); // prevent the form submission
// provide feedback to the user in some way
}
}
});
在表单加载时禁用提交按钮。 仅在勾选复选框时启用它,并在取消选中复选框时再次禁用它 检查以下示例:
<input type="submit" id="btn" disabled="true" value="Submit"/>
<input type="checkbox" id="chk" onchange="submitStatus();">Terms and Conditions</input>
在表单加载时禁用提交按钮。 仅在勾选复选框时启用它,并在取消选中复选框时再次禁用它 检查以下示例:
<input type="submit" id="btn" disabled="true" value="Submit"/>
<input type="checkbox" id="chk" onchange="submitStatus();">Terms and Conditions</input>
您可以使用jquery验证
u可以使用jquery验证
最好的方法是使用JQuery bcoz。如果禁用按钮并仅在选中复选框时启用,用户可能不知道只有选中复选框时按钮才会启用。您可以使用上述相同的编码。最好的方法是使用JQuery bcoz,如果禁用按钮并仅在选中复选框时启用,用户可能不知道只有选中复选框时按钮才会被启用。您可以使用上面提到的相同编码。问题实际上是,一旦按钮被“启用”,我的所有其他验证都将被绕过,表单仍将提交。问题实际上是,一旦按钮被“启用”,我的所有其他验证都将被取消绕过,表单仍将提交…您可能应该向用户反馈导致按钮禁用的原因,但是的,这是另一个好方法。@lonesomeday-谢谢您的建议,我已更新了答案。谢谢!最后我使用了一种不同的方法。您可能应该给用户一些反馈,说明是什么原因导致按钮被禁用,但是是的,这是另一种很好的方法。@lonesomeday-谢谢您的建议,我已经更新了答案。谢谢!最后我使用了一个变体。试着把它作为评论而不是答案输入。这样你会得到更好的反馈。试着把它作为评论而不是答案输入。这样你会得到更好的反馈。