【问题标题】:How to get multiple selected radio buttons value using Jquery如何使用 Jquery 获取多个选定的单选按钮值
【发布时间】:2015-12-22 22:21:44
【问题描述】:

我想使用 Jquery 获取多个单选按钮值。但是现在只能得到第一个的值。

<form action="get" method="post">
                    <table width="100%">
                    <tbody id="checklistwrapper">

                    <tr class="checklisttr" id="1">

                    <td align="left" width="40%"><br>
                    <label for="veryiii" style="align:left;">Within your circle of competence?</label>
                    </td>

                    <td align="center">
                    <label for="veryi" width="12%">Worst</label><br><br>
                    <input type="radio" id="veryi" value="5.0" name="circle">
                    </td>

                    <td align="center" width="12%"><br><br>
                    <input type="radio" id="vii" value="4.5" name="circle">
                    </td>

                    <td align="center" width="12%">
                    <label for="veryiii">Neutral</label><br><br>
                    <input type="radio" id="veryiii" value="3.0" name="circle"> 
                    </td>

                    <td align="center" width="12%"><br><br>
                    <input type="radio" id="viv" value="1.5" name="circle">
                    </td>

                    <td align="center" width="12%">
                    <label for="veryv">Best</label><br><br>
                    <input type="radio" id="veryv" value="1.0" name="circle">
                    </td>
                    </tr>

                    <tr class="checklisttr" id="2">

                    <td align="left" width="40%"><br>
                    <label for="veryiii" style="align:left;">Macro economic environment favorable?</label>
                    </td>

                    <td align="center" width="12%"><br>
                    <input type="radio" id="veryi" value="5.0" name="macro">
                    </td>

                    <td align="center" width="12%"><br>
                    <input type="radio" id="vii" value="4.5" name="macro">
                    </td>

                    <td align="center" width="12%"><br>
                    <input type="radio" id="veryiii" value="3.0" name="macro"> 
                    </td>

                    <td align="center" width="12%"><br>
                    <input type="radio" id="viv" value="1.5" name="macro">
                    </td>

                    <td align="center" width="12%"><br>
                    <input type="radio" id="veryv" value="1.0" name="macro">
                    </td>
                    </tr>

                    </tbody></table>

                    <div style="float:right;margin-top:10px;margin-bottom:5px;">
                        <button type="button" id="savechecklist">Save Checklist</button>
                    </div>

    $('#savechecklist').click(function(){
    var the_value;
    //the_value = jQuery('input:radio:checked').val();
    //the_value = jQuery('input[name=macro]:radio:checked').val();
    the_value = getChecklistItems();
    //alert(the_value);
});

function getChecklistItems() {
    var columns = [];

    $('tr.checklisttr').each(function() {
        //columns.push($(this).('input:radio:checked').val());
        //the_value = $(this).('input:radio:checked').val();
        var the_value = jQuery('input:radio:checked').attr('id');
        alert(the_value);
    });

    return columns.join('|');
}

【问题讨论】:

    标签: jquery radio-button


    【解决方案1】:
    $('#savechecklist').click(function() {
        var the_value;
        //the_value = jQuery('input:radio:checked').val();
        //the_value = jQuery('input[name=macro]:radio:checked').val();
        the_value = getChecklistItems();
        alert(the_value);
    });
    
    function getChecklistItems() {
        var result =
            $("tr.checklisttr > td > input:radio:checked").get();
    
        var columns = $.map(result, function(element) {
            return $(element).attr("id");
        });
    
        return columns.join("|");
    }
    

    http://jsfiddle.net/btG5L/

    【讨论】:

      【解决方案2】:

      更简洁的解决方案是

      $('input:radio:checked').map(function(i, el){return $(el).val();}).get();
      

      (显然你可以在地图之前做更多的过滤。)

      这会给你一个值列表,然后你可以用它做你想做的事情,例如。 .join()

      【讨论】:

        猜你喜欢
        • 2011-05-07
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        相关资源
        最近更新 更多