jQuery get value of selected radio button

前端 未结 27 1563
耶瑟儿~
耶瑟儿~ 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:31

    multi group radio button covert value to array

    var arr = [];
        function r(n) {
    
    
            var section = $('input:radio[name="' + n + '"]:checked').val();
            arr[n] = section;
    
            console.log(arr)
        }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="radio" onchange="r('b1')" class="radioID" name="b1" value="1">1
    <input type="radio" onchange="r('b1')"  class="radioID" name="b1" value="2"  >2
    <input type="radio" onchange="r('b1')"   class="radioID" name="b1" value="3">3
    
    <br>
    
    <input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="4">4
    <input type="radio" onchange="r('b2')"  class="radioID2" name="b2" value="5"  >5
    <input type="radio" onchange="r('b2')"   class="radioID2" name="b2" value="6">6

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

    Here are 2 radio buttons namely rd1 and Radio1

    <input type="radio" name="rd" id="rd1" />
    <input type="radio" name="rd" id="Radio1" /> 
    

    The simplest wayt to check which radio button is checked ,by checking individual is(":checked") property

    if ($("#rd1").is(":checked")) {
          alert("rd1 checked");
       }
     else {
          alert("rd1 not checked");
       }
    
    0 讨论(0)
  • In case you don't know the sepcific name or want to check all radio inputs in a form, you can use a global var to check for each radio group the value only once: `

            var radio_name = "";
            $("form).find(":radio").each(function(){
                if (!radio_name || radio_name != $(this).attr('name')) {
                    radio_name = $(this).attr('name');
                    var val = $('input[name="'+radio_name+'"]:checked').val();
                    if (!val) alert($('input[name="'+radio_name+'"]:checked').val());
                }
            });`
    
    0 讨论(0)
  • 2020-11-22 14:34

    Check the example it works fine

    <div class="dtl_radio">
      Metal purity : 
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="92" checked="">
        92 %
      </label>
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="75">
        75 %
      </label>
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="58.5">
        58.5 %
      </label>
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="95">
        95 %
      </label>
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="59">
        59 %
      </label>
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="76">
        76 %
      </label>
        <label>
        <input type="radio" name="purityradio" class="gold_color" value="93">
        93 %
      </label>
       </div>
    

    var check_value = $('.gold_color:checked').val();

    0 讨论(0)
  • 2020-11-22 14:36
    1. In your code, jQuery just looks for the first instance of an input with name q12_3, which in this case has a value of 1. You want an input with name q12_3 that is :checked.
    $(function(){
        $("#submit").click(function() {     
            alert($("input[name=q12_3]:checked").val());
        });
    });
    
    1. Note that the above code is not the same as using .is(":checked"). jQuery's is() function returns a boolean (true or false) and not an element.
    0 讨论(0)
  • 2020-11-22 14:36
    HTML Code
    <input id="is_verified" class="check" name="is_verified" type="radio" value="1"/>
    <input id="is_verified" class="check" name="is_verified" type="radio" checked="checked" value="0"/>
    <input id="submit" name="submit" type="submit" value="Save" />
    
    Javascript Code
    $("input[type='radio'].check").click(function() {
        if($(this).is(':checked')) {
            if ($(this).val() == 1) {
                $("#submit").val("Verified & Save");
            }else{
                $("#submit").val("Save");
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题