问题陈述很简单。我需要看看用户是否从无线电组中选择了一个单选按钮。组中的每个单选按钮共享相同的id。

问题是我无法控制表单是如何生成的。下面是单选按钮控件代码的样例代码:

<input type="radio" name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >

除此之外,当单选按钮被选中时,它不会添加一个“checked”属性到控件中,只是文本检查(我猜只是没有值的属性检查)。下面是选定的无线电控件的外观

<input type="radio" checked name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >

有人能帮助我用jQuery代码,可以帮助我得到选中单选按钮的值吗?


当前回答

对于多个单选按钮,您必须在单选按钮标签上放置相同的name属性。 例如;

<input type="radio" name"gender" class="select_gender" value="male">
<input type="radio" name"gender" class="select_gender" value="female">

一旦你有了单选选项,现在你可以像下面这样使用jQuery代码来获得选中/选中的单选选项的值。

$(document).on("change", ".select_gender", function () {
   console.log($(this).val());    // Here you will get the current selected/checked radio option value
});

注意:使用$(document)是因为如果单选按钮是在DOM 100%加载后创建的,那么你需要它,因为如果你不使用$(document),那么jQuery将不知道新创建的单选按钮的范围。像这样调用jQuery事件是一个很好的实践。

其他回答

这段代码可以工作。

var radioValue = $("input[name='gender']:checked").val();

对于多个单选按钮,您必须在单选按钮标签上放置相同的name属性。 例如;

<input type="radio" name"gender" class="select_gender" value="male">
<input type="radio" name"gender" class="select_gender" value="female">

一旦你有了单选选项,现在你可以像下面这样使用jQuery代码来获得选中/选中的单选选项的值。

$(document).on("change", ".select_gender", function () {
   console.log($(this).val());    // Here you will get the current selected/checked radio option value
});

注意:使用$(document)是因为如果单选按钮是在DOM 100%加载后创建的,那么你需要它,因为如果你不使用$(document),那么jQuery将不知道新创建的单选按钮的范围。像这样调用jQuery事件是一个很好的实践。

只看名字

$(function () {
    $('input[name="EventType"]:radio').change(function () {
        alert($("input[name='EventType']:checked").val());
    });
});
 $("input[name='gender']:checked").val()

对于嵌套属性

$("input[name='lead[gender]']:checked").val()

不要忘记名字的单大括号

另一种理解…工作:

HTML代码:

 <input type="radio" name="active_status" class="active_status" value="Hold">Hold 
 <input type="radio" name="active_status" class="active_status" value="Cancel">Cancel 
 <input type="radio" name="active_status" class="active_status" value="Suspend">Suspend

Jquery代码:

$(document).on("click", ".active_status", function () {
 var a = $('input[name=active_status]:checked').val();  
 (OR)   
 var a = $('.active_status:checked').val();
 alert(a);
});