javascript - 验证单选按钮被选中

标签 javascript html forms validation radio-button

我一直在搜索并尝试许多不同的方法来使此表单在提交时验证单选按钮,但没有成功。

我有一个名为

的表格
user_details

我有几个广播组,一个是

overall_experience

还有一个是

promptness

我究竟如何获得简单的 javascript 函数 validateForm 来确保最终用户选择了每个组中的一个单选按钮?

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

但是无线校验码不对。

有谁知道如何确保最终用户从每个单选按钮组中至少选择了一个选项?

最佳答案

看看这个希望这是你需要的: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>

并在表单提交时调用 validater()。

关于javascript - 验证单选按钮被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19035776/

相关文章:

javascript - 在数据加载之前加载完整日历

javascript - 如何将单选按钮名称列表传递给 spring Controller 并将它们显示到成功页面

javascript - 当我应用 TinyMCE 时,如何使 textarea 强制提交

html - 单选按钮在 IE7 和 IE9 中未对齐

c# - 我可以提交仅包含隐藏字段的表单吗?

javascript - 在 JS 中通过函数传递变量的最佳方法

javascript - 使用 gmaps api 中的参数调用函数 initmap

html - 无法将中文字符发布到我的搜索表单

javascript - MankSans 字体的 Safari 兼容性字母间距问题

javascript - (图形)树结构中的 jQuery sortables