JavaScript read radio button value in IE and FireFox

前端 未结 5 838
眼角桃花
眼角桃花 2021-01-21 05:02

I have a simple web form that uses JavaScript for building a POST statement. In Chrome, I can use a simple line of code...

var form = document.forms[\'myForm\'];         


        
5条回答
  •  伪装坚强ぢ
    2021-01-21 05:04

    Try this

    function getValueFromRadioButton(name) {
       //Get all elements with the name
       var buttons = document.getElementsByName(name);
       for(var i = 0; i < buttons.length; i++) {
          //Check if button is checked
          var button = buttons[i];
          if(button.checked) {
             //Return value
             return button.value;
          }
       }
       //No radio button is selected. 
       return null;
    }
    

    IDs are unique so you should not use the same ID for multiple items. You can remove the all the radio button IDs if you use this function.

提交回复
热议问题