How to get value of selected radio button?

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

    You can also loop through the buttons with a forEach-loop on the elements

        var elements = document.getElementsByName('radioButton');
        var checkedButton;
        console.log(elements);
        elements.forEach(e => {
            if (e.checked) {
                //if radio button is checked, set sort style
                checkedButton = e.value;
            }
        });
    

提交回复
热议问题