How can we access the value of a radio button using the DOM?

后端 未结 12 1901
鱼传尺愫
鱼传尺愫 2020-12-05 15:25

How can we access the value of a radio button using the DOM?

For eg. we have the radio button as :



        
12条回答
  •  难免孤独
    2020-12-05 15:51

    If you want the selected one, but don't have a framework handy, you can iterate over the element array looking for whichever one is checked:

    for (var i = 0; i < document.form1.sex.length; i++) {
        if (document.form1.sex[i].checked) alert(document.form1.sex[i].value);
    }
    

提交回复
热议问题