Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To do this you have to use the <a href="http://docs.jquery.com/Selectors/checked" rel="noreferrer"><code>:checked</code></a> selector. Although JP's answer is fine, I'd probably do this:</p> <pre><code>$('#form1').submit(function() { if ($('input:checkbox', this).is(':checked') &amp;&amp; $('input:radio', this).is(':checked')) { // everything's fine... } else { alert('Please select something!'); return false; } }); </code></pre> <p>Couple of notes:</p> <ul> <li>I think it reads better to use the <a href="http://docs.jquery.com/Is" rel="noreferrer"><code>is</code></a> function, which returns a boolean of the selector.</li> <li>You can use <a href="http://docs.jquery.com/Selectors/radio" rel="noreferrer"><code>:radio</code></a> and <a href="http://docs.jquery.com/Selectors/checkbox" rel="noreferrer"><code>:checkbox</code></a> as a shortcut for selecting all radios and checkboxes in a form. However, <a href="http://docs.jquery.com/Selectors/checkbox" rel="noreferrer">according to the jQuery manual</a>, it is bad practice to use these selectors without specifying <code>input</code> before them, as they evaluate to <code>*:checkbox</code> and <code>*:radio</code> otherwise, which are very slow selectors.</li> <li>By passing <code>this</code> as the second argument we are <a href="http://docs.jquery.com/Core/jQuery#expressioncontext" rel="noreferrer">specifying a context</a> for the search, and thus are only searching for checkboxes and radio inputs inside the current form. Without it we might get false positives if there happens to be another form in the page.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload