javascript - 验证 jQuery 中的复选框组

标签 javascript c# jquery razor

我正在根据每页 30 行的表中的建议生成复选框。一行可以有 0 个建议,也可能有 100 个建议。我想使用 jQuery 只允许用户选择一个复选框。如果他们选择了一项,然后又改变了主意,则原始复选框将被取消选中,而新复选框将被选中。

enter image description here

最佳答案

您正在寻找的是 radio 输入。

<label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio" value="Test"/>Test</label>

确保一组 radio 输入具有相同的名称! 给它们不同的名称意味着您将能够选择多个 radio 输入:

<label><input type="radio" name="testRadio1" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio2" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio3" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio4" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio5" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio6" value="Test"/>Test</label><br/>
<label><input type="radio" name="testRadio7" value="Test"/>Test</label>

这是不可取的。

如果您确实想要复选框,请使用以下 CSS:

input[type="radio"] {
    -webkit-appearance: checkbox; /* Chrome, Safari, Opera */
    -moz-appearance: checkbox;    /* Firefox */
    -ms-appearance: checkbox;     /* not currently supported */
}
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label><br/>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>
    <label><input type="radio" name="testRadio" value="Test"/>Test</label>

关于javascript - 验证 jQuery 中的复选框组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329914/

相关文章:

javascript - 如何使用 jquery 获取 div 中当前选定的单选按钮的值?

javascript - 为什么我的 PreventDefault 在 Chrome 浏览器 jquery 中不起作用?

c# - 服务器客户端发送/接收多个客户端

javascript - 如何在每个forEach语句之间添加延迟?

c# - 如何编写嵌套泛型函数

c# - 如何在新流程中启动第二个项目的新实例

javascript - 如何为单个图像替换整个 Div 元素(其中包含其他元素)?

c# - 使用jQuery动态加载用户控件到div后如何让javascript执行?

javascript - 使用 JavaScript 禁用下拉菜单

javascript - 不和谐机器人 : struggling to tally emoji reacts for voting