db83
db83

Reputation: 169

jquery prevent multiple selection of radiobutton

I have multiple forms each with a radiobutton group Basically I want to only allow the user to select one of the options once. In my case only allow selection of Yes once

You can see the behaviour in the link below

  1. select Yes option on first row (remains at NO - what i want)
  2. select No option on second row
  3. select Yes option on first row (allowed because no other YES selected)
  4. select Yes option for second row (switches selection to NO on first row instead of remaining at NO)

Seems to ignore the selection in this line of the jquery code

$('input:radio[name=$(tmp)]]')[1].checked = true;

Running example

Upvotes: 0

Views: 914

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You could do something like this :

$("input[name*='records']").click(function(e) {
    var inputs = $("form input[type=radio][value=Y]:checked");
    if (inputs.length > 1){
        e.preventDefault();
    }
});

Fiddle here: http://jsfiddle.net/6EQHE/5/

Upvotes: 2

Patricia
Patricia

Reputation: 7802

change it to:

 $('input:radio[name=$(tmp)]]')[1].attr('checked', 'checked');

Upvotes: 0

Related Questions