JavaScript read radio button value in IE and FireFox

前端 未结 5 841
眼角桃花
眼角桃花 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:12

    Short & clear on ES-2015, for use with Babel:

    function getValueFromRadioButton( name ){
      return [...document.getElementsByName(name)]
             .reduce( (rez, btn) => (btn.checked ? btn.value : rez), null)
    }
    
    console.log( getValueFromRadioButton('payment') );

提交回复
热议问题