简体   繁体   English

当有多个具有相同名称的单选按钮时,如何检查选择了哪个单选按钮? (使用jquery)

[英]How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

<form class="responses">
    <input type="radio" name="choice" value="0">False</input>
    <input type="radio" name="choice" value="1">True</input>
</form>

Have tried: 试过:

$('[name="choice"]').is(':checked'))

If either is checked it returns true, if neither is checked returns false, but I need to see which one is checked. 如果选中其中之一,则返回true,如果两者都未选中则返回false,但我需要查看哪一个被选中。

I am creating choices dynamically, and am not giving each an ID. 我正在动态创建选择,而不是给每个ID。

使用以下代码,您将获得已检查的radio元素的DOM

$("[name=choice]:checked")

The following code will give the value of the checked radio button. 以下代码将给出选中的单选按钮的值。 If nothig is checked it will give undefined. 如果未选中nothig,则将给出undefined。 Put this snippet inside a function and call the function on checking radio button 将此代码段放在函数中,并在选中单选按钮时调用该函数

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

If you want to know the selected value use 如果您想知道所选值的使用方法

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

If you want to know which radio button is selected use 如果您想知道选择了哪个单选按钮,请使用

var radioButton = $("input:radio[name='choice']");
var selectedIndex = radioButton.index(radioButton.filter(':checked'));

Other answers are the perfect one , this can be an another approach 其他答案是完美的,这可能是另一种方法

HTML : HTML:

<form class="responses">
 <input type="radio" name="choice" class="my_radio" value="0">False</input>
 <input type="radio" name="choice" class="my_radio" value="1">True</input>
</form>

JQUERY: JQUERY:

 $('.my_radio').click(function(){
  alert($(this).val());
 });

try this: 尝试这个:

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

Script: 脚本:

$( "#myForm input" ).on( "change", function() {
  $( "#log" ).html( $( "input[name=choice]:checked" ).val() + " is checked!" );
});

HTML: HTML:

<form id="myForm">
    <input type="radio" name="choice" value="0">False</input>
    <input type="radio" name="choice" value="1">True</input>
</form>
<div id="log"></div>

 $( "#myForm input" ).on( "change", function() { $( "#log" ).html( $( "input[name=choice]:checked" ).val() + " is checked!" ); }); 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="myForm"> <input type="radio" name="choice" value="0">False</input> <input type="radio" name="choice" value="1">True</input> </form> <div id="log"></div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查Jinja2 HTML模板中选中的单选按钮中的哪个单选按钮? - How do I check which radio button in a group of radio buttons is selected in a Jinja2 HTML Template? 如何使用 javascript 获取多个单选按钮具有相同名称的选定单选按钮的值 - How to get the value of selected radio button where multiple radio buttons have same name using javascript 如何更改使用功能选择的单选按钮? - How do I change which radio button is selected using a function? 如何使用Javascript检查其他单选按钮中是否选中了特定的单选按钮? - How do i check if a specific Radio button is checked among the other radio buttons using Javascript? 在jQuery中,当它们都具有相同的名称时,如何获得单选按钮的值? - In jQuery, how do I get the value of a radio button when they all have the same name? 当总共选择了5个单选按钮时,如何禁用单选按钮? - How to disable radio buttons when in total 5 radio button are selected? 如何检查选择了哪个单选按钮,并在jQuery中使用if else语句应用适当的行为 - How can I check which radio button is selected and apply an appropriate behaviour with if else statements in jQuery 在jQuery中动态选择时如何获取单选按钮的名称和值? - How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery? 我如何知道通过 jQuery 选择了哪个单选按钮? - How can I know which radio button is selected via jQuery? 如何使用jQuery检查表单中多个动态单选按钮的值 - How to check the values of multiple dynamic radio buttons in a form using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM