如何验证是否至少选择了 1 个单选按钮?
如何验证是否至少选择了 1 个单选按钮?
How can I validate that at least 1 radio button is selected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何验证是否至少选择了 1 个单选按钮?
How can I validate that at least 1 radio button is selected?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
如果所有单选按钮都是同一个 RadioButtonList 的一部分,则可以使用简单的 RequiredFieldValidator。 下面是一个示例:
如果您在不同的 RadioButtonList 中有 RadioButton,并且您想要验证是否在多个列表中至少选择了一个,那么您将需要一个 自定义验证器。
If all the radio buttons are part of the same RadioButtonList, you can use a simple RequiredFieldValidator. Here's an example of that:
If you have RadioButtons in different RadioButtonLists and you want to validate that at least one is selected across multiple lists, then you would need a CustomValidator.
另一种方法可能是,如果您在页面加载期间默认选中其中一个选项(可能是您认为大多数用户会选择的选项),那么用户将“被迫”更改选择或继续使用默认选项,这样您就可以确保始终有一个选项被选中。
Another way might be if you checked one of them by default, during pageload (maybe the one you think most users will select) then the user will be "forced" to change the selection or continue with the default option, in this way you make sure there is always an option selected.