jQuery get value of selected radio button

前端 未结 27 1876
耶瑟儿~
耶瑟儿~ 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:32

    In case you don't know the sepcific name or want to check all radio inputs in a form, you can use a global var to check for each radio group the value only once: `

            var radio_name = "";
            $("form).find(":radio").each(function(){
                if (!radio_name || radio_name != $(this).attr('name')) {
                    radio_name = $(this).attr('name');
                    var val = $('input[name="'+radio_name+'"]:checked').val();
                    if (!val) alert($('input[name="'+radio_name+'"]:checked').val());
                }
            });`
    

提交回复
热议问题