Good day! There is a conditional form with two radiobuttons and a submission:

<form> <input type="radio" name="radio" value="1" id="one" /> <input type="radio" name="radio" value="2" id="two" /> <input type="submit" id="submit" /> </form> 

There are no options to select, the form will not be submitted:

 if($('input[name=radio]').attr('checked') != 'checked')) { alert('Вы не сделали выбор'); return false; } 

But with the sending form when choosing one or another option, problems arose. Tried to hang handlers on click:

 $('#one').click(function() { $('#one').attr('checked', true); }); $('#two').click(function() { $('#two').attr('checked', true); }); 

But only #one fulfills, and when #two is selected, the alert is output again. How to be?

    1 answer 1

    At radio and checkbox attributes do not change at all at change, it is necessary to use the selector :checked :

     if(!$('input[name=radio]:checked').val()) { alert('Вы не сделали выбор'); return false; } 

    And accordingly, there is no need to manually set the attribute on click. You had an error that you set true , which simply put the checked attribute with no value. And you checked it for value.

    • I tried as you suggest: it still works out only when choosing #one. When selecting #two, it gives an alert ((( - humster_spb
    • Sorry, really confused with the checkbox, corrected the answer - Artem Gorlachev
    • Yes, it worked. Thank! - humster_spb