How to get value of selected radio button?

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

提交回复
热议问题