How to get the selected radio button’s value?

后端 未结 18 1596
北海茫月
北海茫月 2020-11-22 00:57

I’m having some strange problem with my JS program. I had this working properly but for some reason it’s no longer working. I just want to find the value of the radio button

18条回答
  •  长发绾君心
    2020-11-22 01:09

    You can do something like this:

    var radios = document.getElementsByName('genderS');
    
    for (var i = 0, length = radios.length; i < length; i++) {
      if (radios[i].checked) {
        // do whatever you want with the checked radio
        alert(radios[i].value);
    
        // only one radio can be logically checked, don't check the rest
        break;
      }
    }
    
    Male
    Female

    jsfiddle

    Edit: Thanks HATCHA and jpsetung for your edit suggestions.

提交回复
热议问题