jQuery 在更改不起作用时获取单选按钮值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19064490/
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-26 23:07:59  来源:igfitidea点击:

get radio button value on change not working

jquery

提问by khkhizar

I use this code for get the value of radio button in jquery, my problem is when I click the option 1 it works fine but when I click on option 2 it not work. Please help!

我使用此代码获取 jquery 中单选按钮的值,我的问题是当我单击选项 1 时它工作正常,但是当我单击选项 2 时它不起作用。请帮忙!

<form>
Option 1<input type="radio" name="opt" id="radio" value="Option 1">
Option 2<input type="radio" name="opt" id="radio" value="Option 2">
</form>

$('#radio').change(function(){
var value = $( 'input[name=opt]:checked' ).val();
alert(value);
});

JSFiddle: http://jsfiddle.net/khizar067/h6ye7/

JSFiddle:http: //jsfiddle.net/khizar067/h6ye7/

回答by Jayesh Goyani

Please try with the below Demo.

请尝试使用下面的演示。

$('input[name=opt]').change(function(){
var value = $( 'input[name=opt]:checked' ).val();
alert(value);
});

http://jsfiddle.net/h6ye7/2/

http://jsfiddle.net/h6ye7/2/

回答by Arun P Johny

IDof elements must be unique, if there are multiple elements with same id then the id selector will return only the first element. so in your case the change event handler is added to only the first radio element

ID元素必须是唯一的,如果有多个元素具有相同的 id,那么 id 选择器将只返回第一个元素。因此,在您的情况下,更改事件处理程序仅添加到第一个单选元素

<form>
Option 1<input type="radio" name="opt" class="radio" value="Option 1">
Option 2<input type="radio" name="opt" class="radio" value="Option 2">
</form>

and

//or $('.radio') as the selector
var $radios = $('input[name=opt]').change(function () {
    var value = $radios.filter(':checked').val();
    alert(value);
});

Demo: Fiddle

演示:小提琴

回答by Greenhorn

The idshould be unique, try using class instead:

id应该是唯一的,请尝试使用类代替:

Html:

网址:

<form>
    Option 1<input type="radio" name="opt" class="radio" value="Option 1">
    Option 2<input type="radio" name="opt" class="radio" value="Option 2">
</form>

Js:

JS:

$('.radio').change(function(){
    var value = $( this ).val();
    alert(value);
});

Demo Fiddle

演示小提琴

回答by Rajdeep Tayde

 <form id="abcd">
     Option 1<input type="radio" name="opt" id="radio" value="Option 1">
     Option 2<input type="radio" name="opt" id="radio" value="Option 2">
 </form>

$('#abcd').on('change','input[name=opt]:checked',function(){
    var value = $(this).val();
    alert(value);
});
  1. id="abcd" is a selector for uniqueness in your whole html code if you have multiple forms it will help to get specific form.
  2. in jQuery for on read the following documentation Visit http://api.jquery.com/on/'change' is the event if this event is occured anytime in the page for the selector "abcd" jquery will search 'input[name=opt]:checked' i.e input type whose name is opt and will check if it is checked and function() is a handler which will handle the event means you can you write your logic here...
  1. id="abcd" 是整个 html 代码中唯一性的选择器,如果您有多个表单,它将有助于获得特定的表单。
  2. 在 jQuery 中阅读以下文档访问http://api.jquery.com/on/'change' 是事件,如果此事件在页面中随时发生在选择器“abcd”的页面中,jquery 将搜索 'input[name= opt]:checked' 即输入类型,其名称为 opt 并将检查它是否被选中并且 function() 是一个处理事件的处理程序,这意味着您可以在此处编写逻辑...

回答by S. S. Rawat

Try this, this is help you, if you use use multiple radio button then you don't need to get id or class of element you simply get element property by there tag name..

试试这个,这对你有帮助,如果你使用多个单选按钮,那么你不需要获取元素的 id 或类,你只需通过标签名称获取元素属性..

There are several example on this

这有几个例子

1.By tag name(All radio button can be select)

1.按标签名称(所有单选按钮都可以选择)

 $('input[type=radio]').change(function(){
        var value = $( 'input[name=opt]:checked' ).val();
        alert(value);
    });

2.By class name(Multiple radio button can be select)

2.按类名(可以选择多个单选按钮)

$('.radiobutton').change(function(){
        var value = $( 'input[name=opt]:checked' ).val();
        alert(value);
    });

3.By id (one radio button select only)

3.By id(仅一个单选按钮选择)

$('#radiobutton').change(function(){
    var value = $( 'input[name=opt]:checked' ).val();
    alert(value);
});

Fiddle Here

小提琴 Here