使用页面上的 jquery 获取单选按钮的所有值

Get all values for radio buttons using jquery on a page

我有 4 个问题,每个问题有两个单选按钮。用户必须为每个问题勾选是或否。

用户必须在所有问题中回答是才能继续使用该应用程序。 使用下面的 jquery 代码,如果用户仅回答 2 个问题,则 tempArray 变量中仅报告 2 个答案。

我希望能够在 tempArray 中看到所有 4 个答案,无论是否已回答。然后我将能够遍历数组并确定 为空或否的答案

例子 全部回答 1.Yes 2.Yes 3.Yes 4.Yes

没有全部回答 1. 是的 2.空 3.空 4.没有

        <script language="javascript" type="text/javascript">    

            $('#Continue').click(function () {

                var $RButtons = $('input[type="radio"]');
                var tempArray = new Array();

                var i = 0;
                jQuery.each($RButtons, function () {
                    if ($(this).is(':checked')) {
                        var value = $(this).attr('value');
                        tempArray[i] = value;
                        i++;
                    }
                });

            })

        </script>

我还没有对此进行测试,但我发现您的代码存在一些问题:

        $('#Continue').click(function () {

            var $RButtons = $('input[type="radio"]');
            var tempArray = new Array();

            var value;
            jQuery.each($RButtons, function () {
                if ($this.prop('checked', true)) { // jQuery 1.6+
                    value = $(this).val();
                    tempArray.push(value);
                }
            });
        })
  1. 您可以使用$this.prop("checked", true)检查单选按钮是否被选中(jQuery 1.6+)
  2. 您应该简单地使用 $(this).val() 来确定复选框的值。
  3. 您不必在外观中使用计数器,只需使用 .push

希望对您有所帮助。

你必须跳过两个。

    var $rb = $('input:radio')
    var tempArray = new Array();
    var questionNo = 0;
    for (var i = 0; i < $rb.length; i += 2) {
      tempArray[questionNo] = "Question " + (questionNo + 1) + " is ";
      if ($($rb[i]).is(':checked')) {            
        tempArray[questionNo] += $($rb[i]).attr('value');
      }
      else if ($($rb[i + 1]).is(':checked')) {
        tempArray[questionNo] += $($rb[i + 1]).attr('value');
      }
      else {
        tempArray[questionNo] += " is null";
      }
      questionNo++;
    }

你可以试试这个。

$('#Continue').click(function () {
              $('div').empty();
                var tempArray = new Array();
                     for ( var i=1; i <= 4; i++)
                     {    
                         var name = "q" + i;
                         var answered = false;
                         $('input:radio[name="' + name + '"]').each(function () {                                  
                          if(!answered)
                          {
                              if($(this).is(":checked"))
                              {
                                  tempArray.push("Question : "  + i + ", Answer : " +  $(this).attr("value"));
                                  answered = true;                                 
                              }                              
                          }
                         });
                         if(!answered)   
                              tempArray.push("Question : "  + i + ", Answer : Null ");
                     }


    $.each(tempArray,function(i)
                 {
                     $('div').append(tempArray[i] + "<br/>");
                 });



            });

工作示例:http://jsfiddle.net/86829ryz/17/