jQuery get value of selected radio button

前端 未结 27 1562
耶瑟儿~
耶瑟儿~ 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:11
    <div id="subscriptions">
     Company Suscription: <input type="radio" name="subsrad" value="1">
     Customer Subscription:<input type="radio" name="subsrad" value="2">
     Manully Set:<input type="radio" name="subsrad" value="MANUAL">
     NO Subscription:<input type="radio" name="subsrad" value="0">
     </div>
    

    and handle jquery for alert as for th e value set / Changed through div id:

    $("#subscriptions input") // select the radio by its id
        .on('change', function(){ // bind a function to the change event
        alert($('input[name="subsrad"]:checked', '#subscriptions').val());
                });
    

    it is so easy....:-}

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

    I am not a javascript person, but I found here for searching this problem. For who google it and find here, I am hoping that this helps some. So, as in question if we have a list of radio buttons:

    <div class="list">
        <input type="radio" name="b1" value="1">
        <input type="radio" name="b2" value="2" checked="checked">
        <input type="radio" name="b3" value="3">
    </div>
    

    I can find which one selected with this selector:

    $('.list input[type="radio"]:checked:first').val();
    

    Even if there is no element selected, I still don't get undefined error. So, you don't have to write extra if statement before taking element's value.

    Here is very basic jsfiddle example.

    0 讨论(0)
  • 2020-11-22 14:13
     $("input[name='gender']:checked").val()
    

    for nested attributes

    $("input[name='lead[gender]']:checked").val()
    

    Don't forget single braces for name

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

    I know that I am joining this late. But it is worth mentioning that it takes

    <label class="custom-control-label" for="myRadioBtnName">Personal Radio Button</label>
    

    And then I checked this in my js. It could be like

    $(document).ready(function () { 
    
    $("#MyRadioBtnOuterDiv").click(function(){
        var radioValue = $("input[name=myRadioButtonNameAttr]:checked").val();
        if(radioValue === "myRadioBtnName"){
            $('#showMyRadioArea').show();
        }else if(radioValue === "yourRadioBtnName"){
            $('#showYourRadioArea').show();
        }
    });
    });
    

    `

    0 讨论(0)
  • 2020-11-22 14:13
        <input type="radio" name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >
    
    $(function() {
          $("#submit").click(function() {
            alert($('input[name=s_2_1_6_0]:checked').val());
          });
        });`
    
    0 讨论(0)
  • 2020-11-22 14:18
    if (!$("#InvCopyRadio").prop("checked") && $("#InvCopyRadio").prop("checked"))
        // do something
    
    0 讨论(0)
提交回复
热议问题