使用 jquery 从广播组获取值

2022-01-21 00:00:00 radio-button jquery html

我正在尝试获取名称为 managerelradio 的无线电组的值.这个电台组的 html 代码是.

I am trying to get value of radio group with name managerelradio. My html code for this radio group is.

 <label><input type="radio" name="managerelradio" value="Yes" id="Add">Add</label>
 <label><input type="radio" name="managerelradio" value="No" id="Remove">Remove</label>

Jquery 是这样的..

and Jquery for this is..

    var manageradiorel = $('input[name = "managerelradio"]:checked' , '#managechildform').val();
 alert(manageradiorel);

它显示我未定义.

虽然我也试过了.

 var manageradiorel = $('input[name = "managerelradio"]:checked').val();
 alert(manageradiorel);

但我仍然得到未定义的值.

But still I am getting undefined value.

推荐答案

试试这个

var manageradiorel = $("input:radio[name ='managerelradio']:checked").val();
alert(manageradiorel);

请检查这个 DEMO ..它会正常工作

Plese check this DEMO ..it will work fine

注意:必须选择您的单选按钮之一.否则会返回 undefined

Note: One of your radio button must be selected. Otherwise it will return undefined

您可以使用 checked 属性将单选按钮设为默认选择

You can use checked attribute to make a radio button selected as default

相关文章