703

问题陈述很简单。我需要查看用户是否从单选组中选择了单选按钮。组中的每个单选按钮共享相同的 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 >

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 >

任何人都可以帮助我使用 jQuery 代码来帮助我获得选中单选按钮的值吗?

4

32 回答 32

1456

就用吧。

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

就这么容易。

于 2014-04-14T06:19:06.973 回答
337

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

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

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

编辑

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

var selectedVal = "";
var selected = $("#radioDiv input[type='radio']:checked");
if (selected.length > 0) {
    selectedVal = selected.val();
}
于 2011-12-24T02:46:57.003 回答
85

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

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

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

于 2017-03-01T14:12:59.010 回答
49
$("#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
        }
    });

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

于 2011-12-24T02:46:46.360 回答
37
  1. 在您的代码中,jQuery 仅查找名称为 q12_3 的输入的第一个实例,在本例中其值为 1。您需要名称为 q12_3 的输入:checked
$(function(){
    $("#submit").click(function() {     
        alert($("input[name=q12_3]:checked").val());
    });
});
  1. 请注意,上面的代码与使用 .is(":checked"). jQuery 的is() 函数返回一个布尔值(真或假)而不是一个元素。
于 2016-03-03T10:17:29.853 回答
35
<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/>

这将返回选中的单选按钮值。

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

我的博客中的更多详细信息:https ://webexplorar.com/jquery-get-selected-radio-button-value/

于 2012-11-05T07:47:51.340 回答
24
 $("input[name='gender']:checked").val()

对于嵌套属性

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

不要忘记名字的单括号

于 2017-11-08T13:04:39.157 回答
21

获取所有收音机:

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

过滤以获取已检查的内容

radios.filter(":checked");

或者

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

var RadeoButtonStatusCheck = $('form input[type=radio]:checked').val();
于 2017-01-23T11:33:27.773 回答
17

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

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

OR by only

$('form input[type=radio]:checked').val();
于 2015-06-18T06:33:04.973 回答
13

此代码有效。

var radioValue = $("input[name='gender']:checked").val();
于 2021-04-11T08:42:50.660 回答
12

使用下面的代码

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

请注意,应该定义 value 属性,以便在结果中获得“Male”或“Female”。

<div id='div_container'>
<input type="radio" name="Gender" value="Male" /> Male <br />
<input type="radio" name="Gender" value="Female" /> Female
</div>
于 2018-11-17T09:49:27.290 回答
12

检查它工作正常的例子

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

于 2015-08-04T07:43:48.283 回答
11

请参阅下面的工作示例,其中包含一组无线电组,每个组与不同的部分相关联。您的命名方案很重要,但理想情况下,您应该尝试对输入使用一致的命名方案(尤其是当它们位于此处这样的部分中时)。

$('#submit').click(function(){
  var section = $('input:radio[name="sec_num"]:checked').val();
  var question = $('input:radio[name="qst_num"]:checked').val();
  
  var selectedVal = checkVal(section, question);
  $('#show_val_div').text(selectedVal);
  $('#show_val_div').show();
});

function checkVal(section, question) {
  var value = $('input:radio[name="sec'+section+'_r'+question+'"]:checked').val() || "Selection Not Made";
  return value;
}
* { margin: 0; }
div { margin-bottom: 20px; padding: 10px; }
h5, label { display: inline-block; }
.small { font-size: 12px; }
.hide { display: none; }
#formDiv { padding: 10px; border: 1px solid black; }
.center { display:block; margin: 0 auto; text-align:center; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="center">
  <div>
    <h4>Section 1</h4>

    <h5>First question text</h5>
    <label class="small"><input type="radio" name="sec1_r1" value="(1:1) YES"> YES</label>
    <label class="small"><input type="radio" name="sec1_r1" value="(1:1) NO"> NO</label>
    <br/>
    <h5>Second question text</h5>
    <label class="small"><input type="radio" name="sec1_r2" value="(1:2) YES"> YES</label>
    <label class="small"><input type="radio" name="sec1_r2" value="(1:2) NO"> NO</label>
    <br/>
    <h5>Third Question</h5>
    <label class="small"><input type="radio" name="sec1_r3" value="(1:3) YES"> YES</label>
    <label class="small"><input type="radio" name="sec1_r3" value="(1:3) NO"> NO</label>
  </div>

  <div>
    <h4>Section 2</h4>
    <h5>First question text</h5>
    <label class="small"><input type="radio" name="sec2_r1" value="(2:1) YES"> YES</label>
    <label class="small"><input type="radio" name="sec2_r1" value="(2:1) NO"> NO</label>
    <br/>
    <h5>Second question text</h5>
    <label class="small"><input type="radio" name="sec2_r2" value="(2:2) YES"> YES</label>
    <label class="small"><input type="radio" name="sec2_r2" value="(2:2) NO"> NO</label>
    <br/>
    <h5>Third Question</h5>
    <label class="small"><input type="radio" name="sec2_r3" value="(2:3) YES"> YES</label>
    <label class="small"><input type="radio" name="sec2_r3" value="(2:3) NO"> NO</label>
  </div>

  <div>
    <h4>Section 3</h4>
    <h5>First question text</h5>
    <label class="small"><input type="radio" name="sec3_r1" value="(3:1) YES"> YES</label>
    <label class="small"><input type="radio" name="sec3_r1" value="(3:1) NO"> NO</label>
    <br/>
    <h5>Second question text</h5>
    <label class="small"><input type="radio" name="sec3_r2" value="(3:2) YES"> YES</label>
    <label class="small"><input type="radio" name="sec3_r2" value="(3:2) NO"> NO</label>
    <br/>
    <h5>Third Question</h5>
    <label class="small"><input type="radio" name="sec3_r3" value="(3:3) YES"> YES</label>
    <label class="small"><input type="radio" name="sec3_r3" value="(3:3) NO"> NO</label>
  </div>
</div>


<div id="formDiv" class="center">
  <form target="#show_val_div" method="post">
    <p>Choose Section Number</p>
    <label class="small">
      <input type="radio" name="sec_num" value="1"> 1</label>
    <label class="small">
      <input type="radio" name="sec_num" value="2"> 2</label>
    <label class="small">
      <input type="radio" name="sec_num" value="3"> 3</label>
    <br/><br/>
    
    <p>Choose Question Number</p>
    <label class="small">
      <input type="radio" name="qst_num" value="1"> 1</label>
    <label class="small">
      <input type="radio" name="qst_num" value="2"> 2</label>
    <label class="small">
      <input type="radio" name="qst_num" value="3"> 3</label>
    <br/><br/>
    
    <input id="submit" type="submit" value="Show Value">
    
  </form>
  <br/>
  <p id="show_val_div"></p>
</div>
<br/><br/><br/>

于 2018-04-14T02:06:50.293 回答
8
if (!$("#InvCopyRadio").prop("checked") && $("#InvCopyRadio").prop("checked"))
    // do something
于 2011-12-24T02:48:03.310 回答
6

用例子试试这个

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1  /jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radio" value="first"/> 1 <br/>
<input type="radio" name="radio" value="second"/> 2 <br/>
</form>



<script>
$(document).ready(function () {
    $('#myForm').on('click', function () {
        var value = $("[name=radio]:checked").val();

        alert(value);
    })
});
</script>
于 2017-06-21T07:28:06.260 回答
6

仅按名称

$(function () {
    $('input[name="EventType"]:radio').change(function () {
        alert($("input[name='EventType']:checked").val());
    });
});
于 2018-02-12T05:43:24.830 回答
6

我知道我加入这么晚了。但值得一提的是,它需要

<label class="custom-control-label" for="myRadioBtnName">Personal Radio Button</label>

然后我在我的js中检查了这个。它可能就像

$(document).ready(function () { 

$("#MyRadioBtnOuterDiv").click(function(){
    var radioValue = $("input[name=myRadioButtonNameAttr]:checked").val();
    if(radioValue === "myRadioBtnName"){
        $('#showMyRadioArea').show();
    }else if(radioValue === "yourRadioBtnName"){
        $('#showYourRadioArea').show();
    }
});
});

`

于 2020-04-13T10:36:16.807 回答
5

您可以在 javascript/jQuery 中使用此 ID 通过 Id 获取所选单选按钮的值。

$("#InvCopyRadio:checked").val();

我希望这将有所帮助。

于 2019-06-20T07:07:31.320 回答
4

解释这个简单主题的最佳方法是提供简单的示例和参考链接:-

在以下示例中,我们有两个单选按钮。如果用户选择任何单选按钮,我们将在警报框中显示选定的单选按钮值。

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

 $(document).ready(function(){
        $('#Demo input').on('change', function() {
            alert($('input[name="Gender"]:checked', '#Demo').val());
        });
    });
于 2014-11-16T14:15:32.320 回答
3
<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());
            });

这很容易......:-}

于 2014-11-26T10:42:32.230 回答
3

这是获取选定单选按钮值的较短版本

var value = $(":radio:checked").val()
于 2021-08-23T14:38:32.607 回答
2

我不是 javascript 人,但我发现这里是为了搜索这个问题。对于谁谷歌它并在这里找到,我希望这会有所帮助。因此,如果我们有一个单选按钮列表,则有问题:

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

我可以找到使用此选择器选择的那个:

$('.list input[type="radio"]:checked:first').val();

即使没有选择任何元素,我仍然没有得到未定义的错误。因此,您不必在获取元素的值之前编写额外的 if 语句。

这是非常基本的 jsfiddle 示例

于 2014-10-25T20:18:55.560 回答
2

正如其他答案所建议的那样,即使input没有必要。也可以使用以下代码:

var variable_name = $("[name='radio_name']:checked").val();
于 2019-12-27T14:55:15.890 回答
2

多组单选按钮将值转换为数组

var arr = [];
    function r(n) {


        var section = $('input:radio[name="' + n + '"]:checked').val();
        arr[n] = section;

        console.log(arr)
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" onchange="r('b1')" class="radioID" name="b1" value="1">1
<input type="radio" onchange="r('b1')"  class="radioID" name="b1" value="2"  >2
<input type="radio" onchange="r('b1')"   class="radioID" name="b1" value="3">3

<br>

<input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="4">4
<input type="radio" onchange="r('b2')"  class="radioID2" name="b2" value="5"  >5
<input type="radio" onchange="r('b2')"   class="radioID2" name="b2" value="6">6

于 2019-04-01T09:50:37.410 回答
2

另一种简单的理解方式......它正在工作:

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);
});
于 2017-10-24T10:13:00.813 回答
1

这里有 2 个单选按钮,即 rd1 和 Radio1

<input type="radio" name="rd" id="rd1" />
<input type="radio" name="rd" id="Radio1" /> 

通过检查单个 is(":checked") 属性来检查选中哪个单选按钮的最简单方法

if ($("#rd1").is(":checked")) {
      alert("rd1 checked");
   }
 else {
      alert("rd1 not checked");
   }
于 2015-12-10T08:55:01.350 回答
1

HTML 标记:

<div class="form-group">
  <input id="rdMale" type="radio" class="clsGender" value="Male" name="rdGender" /> Male
  <input id="rdFemale" type="radio" class="clsGender" value="Female" name="rdGender" /> Female
 </div>

通过单选按钮 ID 获取所选值:

 $("input[name='rdGender']").click(function () {
     alert($("#rdMale").val());
});

通过单选按钮类名获取值

 $(".clsGender").click(function () {
       alert($(this).val());
            //or
      alert($(".clsGender:checked").val());
   });

按名称获取值

   $("input[name='rdGender']").click(function () {
          var rdVaule = $("input[name='rdGender']:checked").val();
          alert(rdVaule);
   });
于 2020-11-22T14:19:19.007 回答
1

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

<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 事件调用是一个好习惯。

于 2021-01-19T07:54:13.860 回答
0
    <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 >

$(function() {
      $("#submit").click(function() {
        alert($('input[name=s_2_1_6_0]:checked').val());
      });
    });`
于 2017-01-06T12:10:54.743 回答
0
HTML Code
<input id="is_verified" class="check" name="is_verified" type="radio" value="1"/>
<input id="is_verified" class="check" name="is_verified" type="radio" checked="checked" value="0"/>
<input id="submit" name="submit" type="submit" value="Save" />

Javascript Code
$("input[type='radio'].check").click(function() {
    if($(this).is(':checked')) {
        if ($(this).val() == 1) {
            $("#submit").val("Verified & Save");
        }else{
            $("#submit").val("Save");
        }
    }
});
于 2018-04-10T04:18:17.143 回答
0
 <div class="col-md-12">
   <div class="form-radio">
       <input class="radio-outlined" id="accept_new_login1" type="radio" name="aNL" value="1">
       <label for="accept_new_login1" class="radio-teal">Yes</label>
        <input class="radio-outlined" id="accept_new_login2" type="radio" name="aNL" value="0">
        <label for="accept_new_login2" class="radio-teal">No</label>
   </div>
</div>

<script>
  $("input[name=aNL]").on("click" , function () {
    console.log("test" ,$("#accept_new_login").val() , this.value , this.id );
  })

 </script>

该解决方案不仅简单,还告诉选定的 ID 和价值。你可以检查我的输出。

在此处输入图像描述

于 2022-01-21T08:37:30.927 回答
0

如果您不知道具体名称或想要检查表单中的所有单选输入,您可以使用全局变量为每个单选组检查一次值:`

        var radio_name = "";
        $("form).find(":radio").each(function(){
            if (!radio_name || radio_name != $(this).attr('name')) {
                radio_name = $(this).attr('name');
                var val = $('input[name="'+radio_name+'"]:checked').val();
                if (!val) alert($('input[name="'+radio_name+'"]:checked').val());
            }
        });`
于 2019-06-26T20:21:55.407 回答