使用动态名称验证单选按钮

时间:2012-12-04 09:05:21

标签: jquery

如何检查所有单选按钮的验证?必须检查每个问题。

<td>QA02</td>
<td>The administrative staff is friendly.</td>
<td><input type="radio" name="QA02" value="1"></td>
<td><input type="radio" name=QA02 value="2"></td>
<td><input type="radio" name=QA02 value="3"></td>
<td><input type="radio" name=QA02 value="4"></td>
<td><input type="radio" name=QA02 value="5"></td>

<td>QA03</td>
<td>The administrative staff shows professionalism in their work.</td>
<td><input type="radio" name="QA03" value="1"></td>
<td><input type="radio" name=QA03 value="2"></td>
<td><input type="radio" name=QA03 value="3"></td>
<td><input type="radio" name=QA03 value="4"></td>
<td><input type="radio" name=QA03 value="5"></td>

1 个答案:

答案 0 :(得分:3)

我给了表格的id,使其能够解决页面的特定部分。

<强> Live Demo

$('#btn').click(function() {
    validationSuccess = true;
    $('#tbl tr td:nth-child(1)').each(function() {
        var group = $(this).text();
        if ($('[name=' + group + ']:checked').length == 0) {
            alert(group + "is not selected");
            validationSuccess = false;
        }
    });
    if(validationSuccess)
    {
        alert("Validation successful");
    }
});​