JQuery: How to get selected radio button value?

前端 未结 11 1159
北海茫月
北海茫月 2020-12-08 06:50

How do I default the value of a non-selected radio button to 0?

I have the following HTML:



        
相关标签:
11条回答
  • 2020-12-08 06:58

    Use the :checked selector to determine if a value is selected:

    function getRadioValue () {
        if( $('input[name=myradiobutton]:radio:checked').length > 0 ) {
            return $('input[name=myradiobutton]:radio:checked').val();
        }
        else {
            return 0;
        }
    }
    

    Update you can call the function above at any time to get the selected value of the radio buttons. You can hook into it on load and then whenever the value changes with the following events:

    $(document).ready( function() {
        // Value when you load the page for the first time
        // Will return 0 the first time it's called
        var radio_button_value = getRadioValue();
    
        $('input[name=myradiobutton]:radio').click( function() {
            // Will get the newly selected value
            radio_button_value = getRadioValue();
        });
    }
    
    0 讨论(0)
  • 2020-12-08 07:00

    I know this is an old question but I find that the answers are not sufficent enough

    The cleanest way to do this is to use this code

    $(".myRadio").click(function() {
    alert($(this).val());   
    });
    

    :-)

    Your form input data should look like this

    <input type="radio" value="40000" name="pay"  class="myRadio" />
    <label for="40000">40000</label>
    
    0 讨论(0)
  • 2020-12-08 07:03

    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-12-08 07:08

    Probably the best method (particularly if you want to be able to post a default value), would be to have a hidden radio button that starts out as selected and has your default value. So something like this:

    <input type="radio" name="myradiobutton" value="0" checked="checked" style="display:none;" />
    <input type="radio" name="myradiobutton" value="1" />1
    <input type="radio" name="myradiobutton" value="2" />2
    <input type="radio" name="myradiobutton" value="3" />3
    

    If you can't modify your html directly, you could add it via script:

    $(function() {
        $('input[name=myradiobutton]:radio:first').before('<input type="radio" name="myradiobutton" value="0" checked="checked" style="display:none;" />');
    });
    

    Here's a demo of that: http://jsfiddle.net/Ender/LwPCv/

    0 讨论(0)
  • 2020-12-08 07:09

    For radio buttons use the following script:

    var myRadio = $('input[name=meme_wall_share]');
    var checkedValue = myRadio.filter(':checked').val();
    
    0 讨论(0)
  • 2020-12-08 07:13

    $('input[name=myradiobutton]:radio:checked') will get you the selected radio button $('input[name=myradiobutton]:radio:not(:checked)') will get you the unselected radio buttons

    Using this you can do this

    $('input[name=myradiobutton]:radio:not(:checked)').val("0");
    

    Update: After reading your Update I think I understand You will want to do something like this

    var myRadioValue;
    
    function radioValue(jqRadioButton){
      if (jqRadioButton.length) {
        myRadioValue = jqRadioButton.val();
      }
      else {
        myRadioValue = 0;
      }
    }
    
    $(document).ready(function () {
      $('input[name=myradiobutton]:radio').click(function () {   //Hook the click event for selected elements
        radioValue($('input[name=myradiobutton]:radio:checked'));
      });
    
      radioValue($('input[name=myradiobutton]:radio:checked')); //check for value on page load
    });
    
    0 讨论(0)
提交回复
热议问题