javascript jquery 在更改时获取单选值

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

jquery get radio value on change

javascriptjqueryhtml

提问by user1936192

i am using: http://blogs.digitss.com/javascript/jquery-javascript/jquery-fancy-custom-radio-and-checkbox/

我正在使用:http: //blogs.digitss.com/javascript/jquery-javascript/jquery-fancy-custom-radio-and-checkbox/

simple code:

简单代码:

<input type="radio" id="a1" class="b1" name="shipping" checked value="single">
<input type="radio" id="a1" class="b1" name="shipping" value="married">
<input type="radio" id="a1" class="b1" name="shipping" value="widowed">

simple head:

简单的头:

$(document).ready(function(){
    $(".radio").dgStyle();
    $(".checkbox").dgStyle();

});

if i add to $(document).ready , after ($".checkbox").dgStyle(); The following:

如果我添加到 $(document).ready 之后 ($".checkbox").dgStyle(); 下列:

alert($('input:radio[name=shipping]:checked').val());

it works fine!

它工作正常!

but - if i do something like that, it is not working:

但是 - 如果我做这样的事情,它不起作用:

$(function() {

    $("input:radio[name=shipping]:checked").change(function() {
        alert("123");
    });

});

i tried to remove :checked, still not working.

我试图删除:检查,仍然无法正常工作。

  • maybe you know different jquery custom buttons in that same colors, which can does it..?
  • 也许你知道相同颜色的不同 jquery 自定义按钮,它可以做什么..?

回答by pala?н

Ok, I got this:

好的,我得到了这个:

$(".radio").click(function() {        
    alert('ID : ' + $(this).find('input:radio').prop('id'));
    alert('Value : ' + $(this).find('input:radio').prop('value'));
});

DEMO HERE

演示在这里

回答by Netorica

You must remove the :checkedon the selector

您必须删除:checked选择器上的

$(function() {
    $("input:radio[name=shipping]").change(function() {
        alert('123');
    });
});

Demo

演示

NOTE: please make sure you don't use same ID's on your elements its really a bad practice or else you will face a lot of issues

注意:请确保不要在元素上使用相同的 ID,这确实是一种不好的做法,否则您将面临很多问题