jQuery get value of selected radio button

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

    See below for working example with a collection of radio groups each associated with different sections. Your naming scheme is important, but ideally you should try and use a consistent naming scheme for inputs anyway (especially when they're in sections like here).

    $('#submit').click(function(){
      var section = $('input:radio[name="sec_num"]:checked').val();
      var question = $('input:radio[name="qst_num"]:checked').val();
      
      var selectedVal = checkVal(section, question);
      $('#show_val_div').text(selectedVal);
      $('#show_val_div').show();
    });
    
    function checkVal(section, question) {
      var value = $('input:radio[name="sec'+section+'_r'+question+'"]:checked').val() || "Selection Not Made";
      return value;
    }
    * { margin: 0; }
    div { margin-bottom: 20px; padding: 10px; }
    h5, label { display: inline-block; }
    .small { font-size: 12px; }
    .hide { display: none; }
    #formDiv { padding: 10px; border: 1px solid black; }
    .center { display:block; margin: 0 auto; text-align:center; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="center">
      <div>
        <h4>Section 1</h4>
    
        <h5>First question text</h5>
        <label class="small"><input type="radio" name="sec1_r1" value="(1:1) YES"> YES</label>
        <label class="small"><input type="radio" name="sec1_r1" value="(1:1) NO"> NO</label>
        <br/>
        <h5>Second question text</h5>
        <label class="small"><input type="radio" name="sec1_r2" value="(1:2) YES"> YES</label>
        <label class="small"><input type="radio" name="sec1_r2" value="(1:2) NO"> NO</label>
        <br/>
        <h5>Third Question</h5>
        <label class="small"><input type="radio" name="sec1_r3" value="(1:3) YES"> YES</label>
        <label class="small"><input type="radio" name="sec1_r3" value="(1:3) NO"> NO</label>
      </div>
    
      <div>
        <h4>Section 2</h4>
        <h5>First question text</h5>
        <label class="small"><input type="radio" name="sec2_r1" value="(2:1) YES"> YES</label>
        <label class="small"><input type="radio" name="sec2_r1" value="(2:1) NO"> NO</label>
        <br/>
        <h5>Second question text</h5>
        <label class="small"><input type="radio" name="sec2_r2" value="(2:2) YES"> YES</label>
        <label class="small"><input type="radio" name="sec2_r2" value="(2:2) NO"> NO</label>
        <br/>
        <h5>Third Question</h5>
        <label class="small"><input type="radio" name="sec2_r3" value="(2:3) YES"> YES</label>
        <label class="small"><input type="radio" name="sec2_r3" value="(2:3) NO"> NO</label>
      </div>
    
      <div>
        <h4>Section 3</h4>
        <h5>First question text</h5>
        <label class="small"><input type="radio" name="sec3_r1" value="(3:1) YES"> YES</label>
        <label class="small"><input type="radio" name="sec3_r1" value="(3:1) NO"> NO</label>
        <br/>
        <h5>Second question text</h5>
        <label class="small"><input type="radio" name="sec3_r2" value="(3:2) YES"> YES</label>
        <label class="small"><input type="radio" name="sec3_r2" value="(3:2) NO"> NO</label>
        <br/>
        <h5>Third Question</h5>
        <label class="small"><input type="radio" name="sec3_r3" value="(3:3) YES"> YES</label>
        <label class="small"><input type="radio" name="sec3_r3" value="(3:3) NO"> NO</label>
      </div>
    </div>
    
    
    <div id="formDiv" class="center">
      <form target="#show_val_div" method="post">
        <p>Choose Section Number</p>
        <label class="small">
          <input type="radio" name="sec_num" value="1"> 1</label>
        <label class="small">
          <input type="radio" name="sec_num" value="2"> 2</label>
        <label class="small">
          <input type="radio" name="sec_num" value="3"> 3</label>
        <br/><br/>
        
        <p>Choose Question Number</p>
        <label class="small">
          <input type="radio" name="qst_num" value="1"> 1</label>
        <label class="small">
          <input type="radio" name="qst_num" value="2"> 2</label>
        <label class="small">
          <input type="radio" name="qst_num" value="3"> 3</label>
        <br/><br/>
        
        <input id="submit" type="submit" value="Show Value">
        
      </form>
      <br/>
      <p id="show_val_div"></p>
    </div>
    <br/><br/><br/>

    0 讨论(0)
  • 2020-11-22 14:20
    $("#radioID") // select the radio by its id
        .change(function(){ // bind a function to the change event
            if( $(this).is(":checked") ){ // check if the radio is checked
                var val = $(this).val(); // retrieve the value
            }
        });
    

    Make sure to wrap this in the DOM ready function ($(function(){...}); or $(document).ready(function(){...});).

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

    Even input is not necessary as suggested by other answers. The following code can also be used:

    var variable_name = $("[name='radio_name']:checked").val();
    
    0 讨论(0)
  • 2020-11-22 14:21

    First, you cannot have multiple elements with the same id. I know you said you can't control how the form is created, but...try to somehow remove all the ids from the radios, or make them unique.

    To get the value of the selected radio button, select it by name with the :checked filter.

    var selectedVal = "";
    var selected = $("input[type='radio'][name='s_2_1_6_0']:checked");
    if (selected.length > 0) {
        selectedVal = selected.val();
    }
    

    EDIT

    So you have no control over the names. In that case I'd say put these radio buttons all inside a div, named, say, radioDiv, then slightly modify your selector:

    var selectedVal = "";
    var selected = $("#radioDiv input[type='radio']:checked");
    if (selected.length > 0) {
        selectedVal = selected.val();
    }
    
    0 讨论(0)
  • 2020-11-22 14:21

    Try this with example

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1  /jquery.min.js"></script>
    <form id="myForm">
    <input type="radio" name="radio" value="first"/> 1 <br/>
    <input type="radio" name="radio" value="second"/> 2 <br/>
    </form>
    
    
    
    <script>
    $(document).ready(function () {
        $('#myForm').on('click', function () {
            var value = $("[name=radio]:checked").val();
    
            alert(value);
        })
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 14:22

    Use Below Code

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

    Please note, value attribute should be defined so could get "Male" or "Female" in your result.

    <div id='div_container'>
    <input type="radio" name="Gender" value="Male" /> Male <br />
    <input type="radio" name="Gender" value="Female" /> Female
    </div>
    
    0 讨论(0)
提交回复
热议问题