How to get value of selected radio button?

前端 未结 28 2173
温柔的废话
温柔的废话 2020-11-22 03:51

I want to get the selected value from a group of radio buttons.

Here\'s my HTML:

相关标签:
28条回答
  • 2020-11-22 04:31

    If you are using jQuery:

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

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

    check value by ID:

    var CheckedValues = ($("#r1").is(':checked')) ? 1 : 0;
    
    0 讨论(0)
  • 2020-11-22 04:32

    This works in IE9 and above and all other browsers.

    document.querySelector('input[name="rate"]:checked').value;
    
    0 讨论(0)
  • 2020-11-22 04:32

    My take on this problem with pure javascript is to find the checked node, find its value and pop it out from the array.

    var Anodes = document.getElementsByName('A'),
        AValue = Array.from(Anodes)
           .filter(node => node.checked)
           .map(node => node.value)
           .pop();
    console.log(AValue);
    

    Note that I'm using arrow functions. See this fiddle for a working example.

    0 讨论(0)
  • 2020-11-22 04:32

    Simply use: document.querySelector('input[rate][checked]').value

    0 讨论(0)
  • 2020-11-22 04:34

    You can get the value by using the checked property.

    var rates = document.getElementsByName('rate');
    var rate_value;
    for(var i = 0; i < rates.length; i++){
        if(rates[i].checked){
            rate_value = rates[i].value;
        }
    }
    
    0 讨论(0)
提交回复
热议问题