验证asp.net中的单个复选框 [英] Validation of single checkbox in asp.net

查看:72
本文介绍了验证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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆