javascript - 是否可以根据以前的单选按钮限制单选按钮的选择?

标签 javascript jquery forms

基本上我有一个调查,我希望人们根据重要性对 5 个项目进行 1-5 的排名。

这是 HTML:http://jsfiddle.net/madcaesar/jj8ky/

<form>
    <table>
    <tr>
                    <th colspan="2" class="altTH" style="text-align:left">Please rank, in order of most important (1) to least important (5)</th>
                  </tr>
                  <tr>
                    <th><label>Professionalism (promoting the profession of medicine)</label></th>
                    <td>
                        <label style="display:inline"><input type="radio" name="Professionalism" value="1" class="checkbox"/> 1</label>
                        <label style="display:inline"><input type="radio" name="Professionalism" value="2" class="checkbox"/> 2</label>
                        <label style="display:inline"><input type="radio" name="Professionalism" value="3" class="checkbox"/> 3</label>
                        <label style="display:inline"><input type="radio" name="Professionalism" value="4" class="checkbox"/> 4</label>
                        <label style="display:inline"><input type="radio" name="Professionalism" value="5" class="checkbox"/> 5</label>
                    </td>
                  </tr>
                  <tr>
                    <th><label>Government Relations (state and federal government advocacy)</label></th>
                    <td>
                        <label style="display:inline"><input type="radio" name="GovernmentRelations" value="1" class="checkbox"/> 1</label>
                        <label style="display:inline"><input type="radio" name="GovernmentRelations" value="2" class="checkbox"/> 2</label>
                        <label style="display:inline"><input type="radio" name="GovernmentRelations" value="3" class="checkbox"/> 3</label>
                        <label style="display:inline"><input type="radio" name="GovernmentRelations" value="4" class="checkbox"/> 4</label>
                        <label style="display:inline"><input type="radio" name="GovernmentRelations" value="5" class="checkbox"/> 5</label>
                    </td>
                  </tr><tr>
                    <th><label>Education (e.g. CME, staff training, meetings)</label></th>
                    <td>
                        <label style="display:inline"><input type="radio" name="Education" value="1" class="checkbox"/> 1</label>
                        <label style="display:inline"><input type="radio" name="Education" value="2" class="checkbox"/> 2</label>
                        <label style="display:inline"><input type="radio" name="Education" value="3" class="checkbox"/> 3</label>
                        <label style="display:inline"><input type="radio" name="Education" value="4" class="checkbox"/> 4</label>
                        <label style="display:inline"><input type="radio" name="Education" value="5" class="checkbox"/> 5</label>
                    </td>
                  </tr>
                  <tr>
                    <th><label>Regulatory and legal analysis and support</label></th>
                    <td>
                        <label style="display:inline"><input type="radio" name="Regulatory" value="1" class="checkbox"/> 1</label>
                        <label style="display:inline"><input type="radio" name="Regulatory" value="2" class="checkbox"/> 2</label>
                        <label style="display:inline"><input type="radio" name="Regulatory" value="3" class="checkbox"/> 3</label>
                        <label style="display:inline"><input type="radio" name="Regulatory" value="4" class="checkbox"/> 4</label>
                        <label style="display:inline"><input type="radio" name="Regulatory" value="5" class="checkbox"/> 5</label>
                    </td>
                  </tr>
                  <tr>
                    <th><label>Communications (Iowa Medicine magazine, website, media relations, etc.)</label></th>
                    <td>
                        <label style="display:inline"><input type="radio" name="Communications" value="1" class="checkbox"/> 1</label>
                        <label style="display:inline"><input type="radio" name="Communications" value="2" class="checkbox"/> 2</label>
                        <label style="display:inline"><input type="radio" name="Communications" value="3" class="checkbox"/> 3</label>
                        <label style="display:inline"><input type="radio" name="Communications" value="4" class="checkbox"/> 4</label>
                        <label style="display:inline"><input type="radio" name="Communications" value="5" class="checkbox"/> 5</label>
                    </td>
                  </tr>

</table>

</form>

一旦 1 被选中一次,jQuery 是否有可能使 1 不可用?和 2 一旦选择了 2 等等?现在他们可以选择他们想要的任何数字,我想不出办法做到这一点。

最佳答案

如果你选择了一个特定的数字,它会取消选择任何其他具有相同值的东西?:

http://jsfiddle.net/jj8ky/2/

$("#radio_table").on("click", ".checkbox", function () {
    var $this = $(this);
    var all = $("#radio_table").find(".checkbox").not($this);
    var val = $this.val();

    if ($this.prop("checked") === true) {
        all.filter(function () {
            return $(this).val() === val;
        }).prop("checked", false);
    }
});

关于javascript - 是否可以根据以前的单选按钮限制单选按钮的选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13711542/

相关文章:

javascript - Angular 2 : Send data from one component to other and act with the data

Javascript 将颜色传递到表名 css 中

javascript - 如何同时使用ajax和普通POST提交表单?

java - 在向服务器提交请求之前验证文本字段

javascript - 如何检查javascript中的类是否存在

javascript - 提交带有 React 输入字段的标准 HTML 表单

javascript - 将更多数据添加到现有模型 (backbone.js)

javascript - CSS如何删除右边、左边和底部的空格

html - 为什么我的jsp request.getParameter() 没有获取到数据?

javascript - 检查 formdata 对象键是否存在