How to get value of selected radio button?

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

    Another (apparently older) option is to use the format: "document.nameOfTheForm.nameOfTheInput.value;" e.g. document.mainForm.rads.value;

    document.mainForm.onclick = function(){
        var radVal = document.mainForm.rads.value;
        result.innerHTML = 'You selected: '+radVal;
    }

    You can refer to the element by its name within a form. Your original HTML does not contain a form element though.

    Fiddle here (works in Chrome and Firefox): https://jsfiddle.net/Josh_Shields/23kg3tf4/1/

提交回复
热议问题