jQuery get value of selected radio button

前端 未结 27 1564
耶瑟儿~
耶瑟儿~ 2020-11-22 13:55

The problem statement is simple. I need to see if user has selected a radio button from a radio group. Every radio button in the group share same id.

The problem is

相关标签:
27条回答
  • 2020-11-22 14:25

    Another Easy way to understand... It's Working:

    HTML Code:

     <input type="radio" name="active_status" class="active_status" value="Hold">Hold 
     <input type="radio" name="active_status" class="active_status" value="Cancel">Cancel 
     <input type="radio" name="active_status" class="active_status" value="Suspend">Suspend
    

    Jquery Code:

    $(document).on("click", ".active_status", function () {
     var a = $('input[name=active_status]:checked').val();  
     (OR)   
     var a = $('.active_status:checked').val();
     alert(a);
    });
    
    0 讨论(0)
  • 2020-11-22 14:28

    You can get the value of selected radio button by Id using this in javascript/jQuery.

    $("#InvCopyRadio:checked").val();
    

    I hope this will help.

    0 讨论(0)
  • 2020-11-22 14:29

    To get the value of the selected Radio Button, Use RadioButtonName and the Form Id containing the RadioButton.

    $('input[name=radioName]:checked', '#myForm').val()
    

    OR by only

    $('form input[type=radio]:checked').val();
    
    0 讨论(0)
  • 2020-11-22 14:29

    By Name only

    $(function () {
        $('input[name="EventType"]:radio').change(function () {
            alert($("input[name='EventType']:checked").val());
        });
    });
    
    0 讨论(0)
  • 2020-11-22 14:29

    The best way to explain this simple topic is by giving simple example and reference link:-

    In the following example we have two radio buttons. If the user selects any radio button, we will display the selected radio button value in a alert box.

    Html:

    <form id="Demo">
    <input type="radio" name="Gender" value="Male" /> Male <br />
    <input type="radio" name="Gender" value="Female" /> Female<br />
    <input type="radio" name="Gender" value="others" /> others <br />
    </form>
    

    jquery:-

     $(document).ready(function(){
            $('#Demo input').on('change', function() {
                alert($('input[name="Gender"]:checked', '#Demo').val());
            });
        });
    
    0 讨论(0)
  • 2020-11-22 14:30

    Just use.

    $('input[name="name_of_your_radiobutton"]:checked').val();
    

    So easy it is.

    0 讨论(0)
提交回复
热议问题