Jquery在更改时获取无线电值

jquery get radio value on change

本文关键字:获取 无线电 Jquery      更新时间:2023-09-26

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

简单代码:

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

简单的头:

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

如果我添加到$(document)。($".checkbox").dgStyle();如下:

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

它工作得很好!

但是-如果我这样做,它不工作:

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

i tried remove:checked, still not working.

  • 也许你知道不同的jquery自定义按钮在相同的颜色,这可以做到…?

好的,我知道了:

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

必须删除选择器上的:checked

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

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