How to get value of selected radio button?

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

    For you people living on the edge:

    There is now something called a RadioNodeList and accessing it's value property will return the value of the currently checked input. This will remove the necessity of first filtering out the 'checked' input as we see in many of the posted answers.

    Example Form

    To retrieve the checked value, you could do something like this:

    var form = document.getElementById("test");
    alert(form.elements["test"].value);
    

    The JSFiddle to prove it: http://jsfiddle.net/vjop5xtq/

    Please note this was implemented in Firefox 33 (All other major browser seems to support it). Older browsers will require a polfyill for RadioNodeList for this to properly function

提交回复
热议问题