【问题标题】:Select specific radio buttons in all groups with no selected checkboxes在没有选中复选框的所有组中选择特定单选按钮
【发布时间】:2015-11-09 05:37:22
【问题描述】:

我需要:

  1. 获取所有未选择选项的单选组
  2. 从这些组中获得带有“数据正确答案”的输入

有没有比我现有的更好的方法? (它正在工作)

jsfiddle

(只需要选择A和H

HTML:

<form class="multiForm trueFalse">
    <ul>
        <li>
            <input data-correctanswer="correct" name="question1" type="radio"><span>A</span>

        </li>
        <li>
            <input name="question1" type="radio"><span>B</span>

        </li>
    </ul>
    <ul>
        <li>
            <input name="question2" type="radio" checked="checked"><span>C</span>

        </li>
        <li>
            <input data-correctanswer="correct" name="question2" type="radio"><span>D</span>

        </li>
    </ul>
    <ul>
        <li>
            <input name="question3" type="radio"><span>E</span>

        </li>
        <li>
            <input data-correctanswer="correct" name="question3" type="radio" checked="checked"><span>F</span>

        </li>
    </ul>
    <ul>
        <li>
            <input name="question4" type="radio"><span>G</span>

        </li>
        <li>
            <input data-correctanswer="correct" name="question4" type="radio"><span>H</span>

        </li>
    </ul>
</form>

JS:

$(document).ready(function () {

    var radio_buttons = $('.multiForm input[name*="question"]').not(':checked');
    var arr = [];
    $('.multiForm ul').each(function () {
        notSelectedRadioGroup = $(this).find('input[name*="question"]');
        if (notSelectedRadioGroup.filter(':checked').length == 0) {
            arr.push($(this));
        }
    });

    $(arr).each(function () {
        $(this).find('input:radio[data-correctanswer]').not(':checked').addClass('selected');
    });
});

【问题讨论】:

    标签: jquery jquery-selectors radio-button radio-group


    【解决方案1】:

    试试,

        $(document).ready(function () {
          var arr = [];
          $('.multiForm ul').each(function () {
            var x = $(":radio", this);
            var cond = x.filter(":not(:checked)").length === x.length;
            if(cond)
             arr.push(x.filter("[data-correctanswer]").data("correctanswer"));         
          });
          alert(JSON.stringify(arr));
        });
    

    DEMO

    $(document).ready(function () {
        $('.multiForm ul').each(function () {
            var x = $(":radio", this);
            var cond = x.filter(":not(:checked)").length === x.length;
            if (cond)
              x.filter("[data-correctanswer]").closest("li").css("color","red");
        });     
    });
    

    DEMO II

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 2015-09-29
      相关资源
      最近更新 更多