1

我有一个带有问题的表格,需要验证这些问题,用单选按钮回答,这个表格有 20 个或更多问题,问题有选项 1、2、3。1 = 好,2 = 问题,3 = 批评。用户只能检查 5 次选项 3. 我怎么能用 javascript 做到这一点?

我只对未选中的选项使用正常验证。

这里是收音机的预览: JSFiddle

<div class="inline">
 <label for="option1">
  <input type="radio" name="group[1]" value="1">1</label>
 <label for="option1">
    <input type="radio" name="group[1]" value="1">2</label>      
 <label for="option1">
    <input type="radio" name="group[1]" value="1">3</label>
</div>

CSS

.inline {
    display: inline;    
}

.inline label, input[type="radio"] {
    float: left;
}

.inline label {
    display: block;
    background: #eee;
    margin-left: 5px;
    border-radius: 5px;
    padding-left: 5px;
    padding-right: 5px;
    padding-top: 3px;
    padding-bottom: 3px;
    color: #333;
    font-family: arial, sans-serif;
}

.inline input[type="radio"] {
    display: block;
}
4

1 回答 1

2

我猜value="1"每台收音机都是错字,你的意思是

<label for="option1">
<input type="radio" id="option1" name="group[1]" value="1">1</label>
<label for="option2">
<input type="radio" id="option2" name="group[1]" value="2">2</label>      
<label for="option3">
<input type="radio" id="option3" name="group[1]" value="3">3</label>

然后您可以找到有多少已选中的带有 value="3" 的收音机正在使用

jQuery('input[value="3"]:checked').length

如果长度大于 5,则采取相应措施。

编辑:jsfiddle 演示(限制设置为 2,而不是 5)。

于 2013-10-25T14:02:38.123 回答