jQuery jQuery获取所选单选按钮的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8622336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:46:46  来源:igfitidea点击:

jQuery get value of selected radio button

jqueryradio-buttonsiebel

提问by user1114212

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). Below is how a selected radio control looks like

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). 下面是选定的无线电控件的样子

<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 代码吗?

回答by Parveen Verma

Just use.

就用吧。

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

So easy it is.

就这么简单。

回答by Adam Rackis

First, you cannot have multiple elements with the same id. I know you said you can't control how the form is created, but...try to somehow remove all the ids from the radios, or make them unique.

首先,不能有多个具有相同 id 的元素。我知道您说过您无法控制表单的创建方式,但是...尝试以某种方式从收音机中删除所有 id,或者使它们独一无二。

To get the value of the selected radio button, select it by name with the :checkedfilter.

要获取所选单选按钮的值,请使用:checked过滤器按名称选择它。

var selectedVal = "";
var selected = $("input[type='radio'][name='s_2_1_6_0']:checked");
if (selected.length > 0) {
    selectedVal = selected.val();
}

EDIT

编辑

So you have no control over the names. In that case I'd say put these radio buttons all inside a div, named, say, radioDiv, then slightly modify your selector:

因此,您无法控制名称。在这种情况下,我会说将这些单选按钮全部放在一个 div 中,命名为radioDiv,然后稍微修改您的选择器:

var selectedVal = "";
var selected = $("#radioDiv input[type='radio']:checked");
if (selected.length > 0) {
    selectedVal = selected.val();
}

回答by Hardik

Simplest way to get the selected radio button's value is as follows:

获取所选单选按钮值的最简单方法如下:

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

No space should be used in between selector.

选择器之间不应使用空格。

回答by Purag

$("#radioID") // select the radio by its id
    .change(function(){ // bind a function to the change event
        if( $(this).is(":checked") ){ // check if the radio is checked
            var val = $(this).val(); // retrieve the value
        }
    });

Make sure to wrap this in the DOM ready function ($(function(){...});or $(document).ready(function(){...});).

确保将其包装在 DOM 就绪函数($(function(){...});$(document).ready(function(){...});)中。

回答by Madhuka Dilhan

  1. In your code, jQuery just looks for the first instance of an input with name q12_3, which in this case has a value of 1. You want an input with name q12_3 that is :checked.
  1. 在您的代码中,jQuery 只查找名称为 q12_3 的输入的第一个实例,在本例中其值为 1。您需要名称为 q12_3 的输入:checked
$(function(){
    $("#submit").click(function() {     
        alert($("input[name=q12_3]:checked").val());
    });
});
  1. Note that the above code is not the same as using .is(":checked"). jQuery's is()function returns a boolean (true or false) and not an element.
  1. 请注意,上面的代码与使用 .is(":checked"). jQuery 的is()函数返回一个布尔值(真或假)而不是一个元素。

回答by Sumith Harshan

<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);
}

回答by Sumith Harshan

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

for nested attributes

对于嵌套属性

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

Don't forget single braces for name

不要忘记名称的单大括号

回答by Faisal

Get all radios:

获取所有收音机:

var radios = $("input[type='radio']");

Filter to get the one that's checked

过滤以获取选中的那个

radios.filter(":checked");

OR

或者

Another way you can find radio button value

您可以找到单选按钮值的另一种方法

var RadeoButtonStatusCheck = $('form input[type=radio]:checked').val();

回答by Nadir

To get the value of the selected Radio Button, Use RadioButtonName and the Form Id containing the RadioButton.

要获取所选单选按钮的值,请使用 RadioButtonName 和包含 RadioButton 的 Form Id。

$('input[name=radioName]:checked', '#myForm').val()

OR by only

或仅由

$('form input[type=radio]:checked').val();

回答by Rajnesh Thakur

Check the example it works fine

检查示例它工作正常

<div class="dtl_radio">
  Metal purity : 
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="92" checked="">
    92 %
  </label>
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="75">
    75 %
  </label>
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="58.5">
    58.5 %
  </label>
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="95">
    95 %
  </label>
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="59">
    59 %
  </label>
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="76">
    76 %
  </label>
    <label>
    <input type="radio" name="purityradio" class="gold_color" value="93">
    93 %
  </label>
   </div>

var check_value = $('.gold_color:checked').val();