jQuery获取所选单选按钮的值

本文讨论如何在无法控制表单生成方式的情况下,利用jQuery获取具有相同ID的单选按钮组中选中的按钮值。示例代码和讨论包括如何检查选中的单选按钮,并提供了一些简单的jQuery解决方案来获取选中按钮的值。
摘要由CSDN通过智能技术生成

本文翻译自:jQuery get value of selected radio button

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. 组中的每个单选按钮共享相同的ID。

The problem is that I don't have control on how the form is generated. 问题是我无法控制表单的生成方式。 Here is the sample code of how a radio button control code looks like: 这是单选按钮控制代码的示例代码:

<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 >

In addition to this when a radio button is selected it doesn't add a "checked" attribute to the control just text checked (I guess just the property checked without a value) . 除此之外,当选择单选按钮时,它不会向控件添加“ checked”属性,而只是对文本进行检查 (我猜只是对属性进行了检查,没有值) Below is how a selected radio control looks like 以下是选定的无线电控件的外观

<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 >

Can anybody help me with jQuery code that can help me to get the value of checked radio button? 有人可以帮助我使用可以帮助我获取选中的单选按钮值的jQuery代码吗?


#1楼

参考:https://stackoom.com/question/aB3w/jQuery获取所选单选按钮的值


#2楼

<input type="radio" class="radioBtnClass" name="numbers" value="1" />1<br/>
<input type="radio" class="radioBtnClass" name="numbers" value="2" />2<br/>
<input type="radio" class="radioBtnClass" name="numbers" value="3" />3<br/>

This will return, checked radio button value. 这将返回选中的单选按钮值。

if($("input[type='radio'].radioBtnClass").is(':checked')) {
    var card_type = $("input[type='radio'].radioBtnClass:checked").val();
    alert(card_type);
}

#3楼

Just use. 随便使用。

$('input[name=name_of_your_radiobutton]:checked').val();

So easy it is. 很容易。


#4楼

I am not a javascript person, but I found here for searching this problem. 我不是一个JavaScript的人,但是我在这里找到了搜索此问题的人。 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: 因此,就像我们是否有单选按钮列表一样:

<div class="list">
    <input type="radio" name="b1" value="1">
    <input type="radio" name="b2" value="2" checked="checked">
    <input type="radio" name="b3" value="3">
</div>

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. 因此,在获取元素值之前,您不必编写额外的if语句。

Here is very basic jsfiddle example . 这是非常基本的jsfiddle示例


#5楼

The best way to explain this simple topic is by giving simple example and reference link:- 解释此简单主题的最佳方法是给出简单的示例和参考链接:-

In the following example we have two radio buttons. 在下面的示例中,我们有两个单选按钮。 If the user selects any radio button, we will display the selected radio button value in a alert box. 如果用户选择任何单选按钮,我们将在警报框中显示选定的单选按钮值。

Html: HTML:

<form id="Demo">
<input type="radio" name="Gender" value="Male" /> Male <br />
<input type="radio" name="Gender" value="Female" /> Female<br />
<input type="radio" name="Gender" value="others" /> others <br />
</form>

jquery:- jQuery的:-

 $(document).ready(function(){
        $('#Demo input').on('change', function() {
            alert($('input[name="Gender"]:checked', '#Demo').val());
        });
    });

#6楼

<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>

and handle jquery for alert as for th e value set / Changed through div id: 并处理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());
            });

it is so easy....:-} 这很容易。...:-}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值