如何验证是否至少选择了 1 个单选按钮?

发布于 2024-07-19 20:28:41 字数 27 浏览 2 评论 0原文

如何验证是否至少选择了 1 个单选按钮?

How can I validate that at least 1 radio button is selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-07-26 20:28:41

如果所有单选按钮都是同一个 RadioButtonList 的一部分,则可以使用简单的 RequiredFieldValidator。 下面是一个示例:

<asp:RadioButtonList 
    ID="RadioButtonList1"
    runat="server"
    RepeatColumns="3">
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Yellow</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator 
    ID="ReqiredFieldValidator1"
    runat="server"
    ControlToValidate="RadioButtonList1"
    ErrorMessage="You must Select your favorite color!">*
</asp: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:

<asp:RadioButtonList 
    ID="RadioButtonList1"
    runat="server"
    RepeatColumns="3">
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Yellow</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator 
    ID="ReqiredFieldValidator1"
    runat="server"
    ControlToValidate="RadioButtonList1"
    ErrorMessage="You must Select your favorite color!">*
</asp:RequiredFieldValidator>

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.

浅唱々樱花落 2024-07-26 20:28:41

另一种方法可能是,如果您在页面加载期间默认选中其中一个选项(可能是您认为大多数用户会选择的选项),那么用户将“被迫”更改选择或继续使用默认选项,这样您就可以确保始终有一个选项被选中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文