单选按钮组

时间:2014-10-10 14:08:06

标签: javascript jquery radio-button

我有一个单选按钮组,如果检查了5个收音机,我试图显示按钮,但我不能这样做。这是我的代码:

$(document).ready(function () {
    $('#vote_submit_button').hide();
    var unanswered = new Array();
    $('table').each(function () {
        var question = $(this).find('input').attr('name');
        if (!$(this).find('input').is(':checked')) {
            unanswered.push(question);
        }
    });

    if (unanswered.length > 0) {
    } else {
        $('#vote_submit_button').show();
    }
});

请帮助)

2 个答案:

答案 0 :(得分:1)

您的代码正在准备好文档,当然所有问题都无法解答。

您需要将事件处理程序绑定到单选按钮

$(function(){
   $('#vote_submit_button').hide();
   var answered = new Array();
   $('input:radio').change(function(){
      var $this= $(this);
      if ($this.is(':checked')){
        anwered.push($this.attr('name'));
      }

      if (answered.length == $('table').length) {
         $('#vote_submit_button').show();
      }

   });
});

答案 1 :(得分:0)

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script>


$(document).ready(function () {

    $(".radio").click(function(){

        $('#vote_submit_button').hide();
        var unanswered = new Array();
        $('table').each(function () {
            var question = $(this).find('input').attr('name');
            if (!$(this).find('input').is(':checked')) {
                unanswered.push(question);
            }
        });

        if (unanswered.length > 0) {
        } else {
            $('#vote_submit_button').show();
        }

    });
});


</script>

<table>
    <tr><td>

        <input type="radio" name="radio1" class="radio">

    </td></tr>
</table>

<table>
    <tr><td>

        <input type="radio" name="radio2" class="radio">

    </td></tr>
</table>

<table>
    <tr><td>

        <input type="radio" name="radio3" class="radio">

    </td></tr>
</table>

<table>
    <tr><td>

        <input type="radio" name="radio4" class="radio">

    </td></tr>
</table>

<table>
    <tr><td>

        <input type="radio" name="radio5" class="radio">

    </td></tr>
</table>

<input type="button" id="vote_submit_button" value="submit" style="display:none;">