问题陈述很简单。我需要看看用户是否从无线电组中选择了一个单选按钮。组中的每个单选按钮共享相同的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事件是一个很好的实践。
<div id="subscriptions">
Company Suscription: <input type="radio" name="subsrad" value="1">
Customer Subscription:<input type="radio" name="subsrad" value="2">
Manully Set:<input type="radio" name="subsrad" value="MANUAL">
NO Subscription:<input type="radio" name="subsrad" value="0">
</div>
和处理jquery警报的值设置/通过div id更改:
$("#subscriptions input") // select the radio by its id
.on('change', function(){ // bind a function to the change event
alert($('input[name="subsrad"]:checked', '#subscriptions').val());
});
这是如此简单....:-}