Bootstrap radio button: Get selected value on submit form

后端 未结 6 1562
春和景丽
春和景丽 2021-02-04 00:20

I have the next boostrap radio-button:

相关标签:
6条回答
  • 2021-02-04 00:45

    In the form submit handler use the :checked selector

    var filterDay = $('#filterDay input:radio:checked').val()
    
    0 讨论(0)
  • 2021-02-04 00:48

    You should set a common "name" to identify the form:

    <div class="btn-group" id="filterDay" data-toggle="buttons">
       <label class="btn btn-default blue">
         <input type="radio" class="toggle" name="toggle" value="1">Monday
       </label>
       <label class="btn btn-default blue">
         <input type="radio" class="toggle" name="toggle" value="2"> Tuesday
       </label>
       <label class="btn btn-default blue">
         <input type="radio" class="toggle" name="toggle" value="3"> Wednesday
       </label>
       <label class="btn btn-default blue">
         <input type="radio" class="toggle" name="toggle" value="4"> Thursday
       </label>
       <label class="btn btn-default blue">
          <input type="radio" class="toggle" name="toggle" value="5"> Friday
       </label>
       <label class="btn btn-default blue">
           <input type="radio" class="toggle" name="toggle" value="6"> Saturday
        </label>
       <label class="btn btn-default blue active">
           <input type="radio" class="toggle" name="toggle" value="0"> Sunday
       </label>
    </div>
    

    And to get the value, you can get like this:

    $('input[name=toggle]:checked').val()
    
    0 讨论(0)
  • 2021-02-04 00:51

    use jquery :checked selector link

    0 讨论(0)
  • 2021-02-04 00:58

    Try this out, It should solve your problem.

    $( "input:checked" ).val()
    

    you can refer the link below for more details:
    http://api.jquery.com/checked-selector/

    0 讨论(0)
  • 2021-02-04 01:01

    Some logic problem in the code...

    <label class="btn btn-default blue active">
           <input type="radio" class="toggle" value="0"> Sunday
    </label>
    

    Label has the active class so bootstrap will display it as active but the contained input radio is not checked, so in pure html it is not checked.

    Correct code...

    <label class="btn btn-default blue active">
           <input type="radio" checked="checked" class="toggle" value="0"> Sunday
    </label>
    
    0 讨论(0)
  • 2021-02-04 01:01

    $('#radioButtonName').prop('checked')

    this works for

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