Set radio button 'checked' in jquery based on ID
I have two radio buttons with the same name, one is checked by default. How can yo开发者_StackOverflow中文版u check or uncheck a radio button in jQuery when selecting from id?
I've tried:
$('#radio1').attr('checked','checked');
$('#radio1').attr('checked', true);
Nothing seems to work.. any ideas?
Thank you!
You can not have same id (#radio1
) more than once, use a class
instead.
$('.radio1').attr('checked', true);
$('.radio2').attr('checked', true);
The id
should be used once per element per page.
If you want to check/uncheck on click
however, you may do like:
$('#someid').click(function(){
$('#radio1').attr('checked', true);
});
Or
$('#someid').click(function(){
$('#radio1').attr('checked', this.checked);
});
If you have the radios like:
<input type="radio" id="radio1" name="same-radio-name" />
<input type="radio" id="radio2" name="same-radio-name" />
So, to check the second and uncheck the first one:
$('#radio2').attr('checked', true);
Viceversa:
$('#radio1').attr('checked', true);
Just another thought, are you using the radio as a JQuery UI button? If so you may also need to refresh it like:
$('#radio1').attr('checked', true).button("refresh");
Well, if you wanna get a value from the input radio to upload this value from your database you can try with this simple code:
var selValue = $('input[name=rbnNumber]:checked').val();
Where you put the name of the variable "selValue" and inside of this you specific the name of the radio group "rbnNumber". All this inside the function that you work and done.
精彩评论