问题
I have 6 forms and each form has 2 radio buttons.
I want the user experience to be, that they can only select 1 radio button at a time. So when they traverse over into another form and check a radio button. all other radio buttons will deselect.
This is what I have so far in my attemps.
$(document).ready(function () {
$('input[type=radio]').prop('checked', false);
$('input[type=radio]').each(function (i) {
$(this).not($('radio:checked')).prop('checked', false);
});
});
my idea has been to deselect all buttons at the start. but after that i'm confused in my logic.
I want to say
if this radio is check, uncheck all other radios.
while in english it fits in one sentence, how can i get it into jquery?
回答1:
$('input[type=radio]').on('change', function(){
$('input[type=radio]').not(this).prop('checked', false);
});
FIDDLE
回答2:
Essentially you can't "unclick" a radio button - so all you have to do is on the "click" event, unselect all radio elements and select the one that was clicked.
$('input[type=radio]').on('click',function(e){
$('input[type=radio]').prop('checked','');
$(this).prop('checked','checked');
e.preventDefault();
});
jsFiddle
回答3:
Try this (see Demo: http://jsfiddle.net/3xD7V/1/):
$(document).ready(function () {
$('input[type=radio]').prop('checked', false);
$('input[type=radio]:first').prop('checked', true)
$('input[type=radio]').click(function (event) {
$('input[type=radio]').prop('checked', false);
$(this).prop('checked', true);
event.preventDefault();
});
});
来源:https://stackoverflow.com/questions/11696339/jquery-select-only-one-radio-from-6-forms