How to get value of selected radio button?

前端 未结 28 2255
温柔的废话
温柔的废话 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:57

    var rates = document.getElementById('rates').value;
    

    The rates element is a div, so it won't have a value. This is probably where the undefined is coming from.

    The checked property will tell you whether the element is selected:

    if (document.getElementById('r1').checked) {
      rate_value = document.getElementById('r1').value;
    }
    

提交回复
热议问题