Getting the selected radio without using “Id” but “name”

后端 未结 6 676
逝去的感伤
逝去的感伤 2020-12-19 03:17

I was trying to get selected radio button by using \"document.getElementByName(\'nameOfradio\')\" because all of the radio buttons share the same name. But, nothing happened

6条回答
  •  长情又很酷
    2020-12-19 03:22

    This is exactly why you should use a javascript library.

    document.querySelector('input[name=nameOfradio]');
    

    for example is not supported before IE8.

    Let the library handle the browser craziness.

    In jQuery you can just use $('input[name=radioName]:checked').val() or $("form").serialize() and be done with it.

提交回复
热议问题