admin管理员组

文章数量:1288075

i am using: /

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:

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

i am using: http://blogs.digitss./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:

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..?
Share Improve this question asked Dec 30, 2012 at 16:16 user1936192user1936192 3452 gold badges4 silver badges10 bronze badges 5
  • 1 Works fine for me: jsfiddle/vsk3V – epascarello Commented Dec 30, 2012 at 16:20
  • do you want it like this? jsfiddle/ds4Qg/2 – Netorica Commented Dec 30, 2012 at 16:21
  • i got into conclusion that because i use a custom styled radio, i can't call change() function. if i remove the custom radio, it works fine. do you know a way to pass through it? or perhaps another way to make custom buttons which i can actually see their change. thanks! – user1936192 Commented Dec 30, 2012 at 16:43
  • OMG! its a div! and not really a checkbox or radio button.. – Netorica Commented Dec 30, 2012 at 16:46
  • really? weird... you know what i can do about it anyway? – user1936192 Commented Dec 30, 2012 at 16:51
Add a ment  | 

2 Answers 2

Reset to default 6

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

You must remove the :checked on the selector

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

本文标签: javascriptjquery get radio value on changeStack Overflow