jQuery get value of selected radio button

前端 未结 27 2016
耶瑟儿~
耶瑟儿~ 2020-11-22 13:55

The problem statement is simple. I need to see if user has selected a radio button from a radio group. Every radio button in the group share same id.

The problem is

27条回答
  •  甜味超标
    2020-11-22 14:11

    I am not a javascript person, but I found here for searching this problem. For who google it and find here, I am hoping that this helps some. So, as in question if we have a list of radio buttons:

    I can find which one selected with this selector:

    $('.list input[type="radio"]:checked:first').val();
    

    Even if there is no element selected, I still don't get undefined error. So, you don't have to write extra if statement before taking element's value.

    Here is very basic jsfiddle example.

提交回复
热议问题