How to get the selected radio button’s value?

后端 未结 18 1614
北海茫月
北海茫月 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:21

    Try this

    function findSelection(field) {
        var test = document.getElementsByName(field);
        var sizes = test.length;
        alert(sizes);
        for (i=0; i < sizes; i++) {
                if (test[i].checked==true) {
                alert(test[i].value + ' you got a value');     
                return test[i].value;
            }
        }
    }
    
    
    function submitForm() {
    
        var genderS =  findSelection("genderS");
        alert(genderS);
        return false;
    }
    

    A fiddle here.

提交回复
热议问题