选择验证单选按钮

时间:2013-09-26 18:21:38

标签: javascript html forms validation radio-button

我一直在寻找并尝试了许多不同的方法来让这个表单验证提交工作的单选按钮没有运气。

我有一个名为

的表单
user_details

我有几个广播组,一个是

overall_experience

另一个是

promptness

我如何在世界上获得简单的javascript函数validateForm以确保最终用户选择了每个组中的一个单选按钮?

我有这个(见这里的屏幕截图) http://michaeljbaran.com/script.png

但无线电检查代码不正确。

有没有人知道如何确保最终用户从每个广播组中选择了至少一个选项?

1 个答案:

答案 0 :(得分:1)

看看这个希望这就是你所需要的:jsfiddle

<table width="440" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <input type="radio" name="overall_experience" value="1" class="a" />1</td>
        <td>
            <input type="radio" name="overall_experience" value="2" class="a" />2</td>
        <td>
            <input type="radio" name="overall_experience" value="3" class="a" />3</td>
        <td>
            <input type="radio" name="overall_experience" value="4" class="a" />4</td>
        <td>
            <input name="overall_experience" type="radio" value="5" class="a" />5</td>
    </tr>
</table>
<br />
</div>
</div>
<div style="width:100%;clear:both;">
    <div class="fullwidth_question"> <span>2. The promptness...they got there when they said they would.</span> 
    </div>
    <div class="fullwidth_answer_indented">
        <table width="440" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <input type="radio" name="promptness" value="1" class="b" />1</td>
                <td>
                    <input type="radio" name="promptness" value="2" class="b" />2</td>
                <td>
                    <input type="radio" name="promptness" value="3" class="b" />3</td>
                <td>
                    <input type="radio" name="promptness" value="4" class="b" />4</td>
                <td>
                    <input type="radio" name="promptness" value="5" class="b" />5</td>
            </tr>
            <input type="button" value="butt" id="butt">

和相关的jquery是:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">  </script>
    <script type = "text/javascript">
    $(document).ready(function validater() {
    $("#butt").click(function (event) {
        if($(".a").is(':checked') && $(".b").is(':checked') ){
           alert("ok");
            return true;}
        else{alert("nok");
      return false;}
    });
 });
 </script>

并在表单的onSubmit上调用validater()。