JQuery: How to get selected radio button value?

前端 未结 11 1169
北海茫月
北海茫月 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 07:14
    jQuery("input:radio[name=myradiobutton]:checked").val();
    
    0 讨论(0)
  • 2020-12-08 07:17

    You should really be using checkboxes if there will be an instance where something isn't selected.

    according to the W3C

    If no radio button in a set sharing the same control name is initially "on", user agent behavior for choosing which control is initially "on" is undefined. Note. Since existing implementations handle this case differently, the current specification differs from RFC 1866 ([RFC1866] section 8.1.2.4), which states:

    At all times, exactly one of the radio buttons in a set is checked. If none of the elements of a set of radio buttons specifies `CHECKED', then the user agent must check the first radio button of the set initially.

    Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on".

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

    It will start as soon as the page loads. You can keep it under some events like button click

    $("#btn").click(function() { 
    var val= $('input[type="radio"]:checked').val();
    });
    
    0 讨论(0)
  • 2020-12-08 07:20

    This work for me hope this will help: to get radio selected value you have to use ratio name as selector like this

    selectedVal = $('input[name="radio_name"]:checked').val();
    

    selectedVal will have the required value, change the radio_name according to yours, in your case it would b "myradiobutton"

    selectedVal = $('input[name="myradiobutton"]:checked').val();
    
    0 讨论(0)
  • 2020-12-08 07:21

    This Jquery method returns the default vale 0 when page loads...

    $('input[type="radio"]:checked').val();
    
    0 讨论(0)
提交回复
热议问题