Note that there are some explanatory texts on larger screens.

plurals
  1. PODon't submit form if nothing selected, using jQuery ajaxForm plugin
    text
    copied!<p>I'm using the <a href="http://malsup.com/jquery/form/" rel="nofollow noreferrer">jQuery Form plugin</a> to do an 'ajax' submit of my form, which consists of about 4 radio selects. However, I want the form to not submit if none of the radio buttons have been selected.</p> <p>Here's my attempt so far, helped by so code I've found in another SO answer and the plugin docs:</p> <pre><code>$(document).ready(function() { var options = { target: '#response', type: 'get', beforeSubmit: ischecked } $('#answer').ajaxForm(options); }); function ischecked(formData, jqForm, options){ if ($('input:radio', $('#answer').is(':checked'))) { return true; } else { return false; } } </code></pre> <p>I'm sure that the callback <code>ischecked</code> is getting executed ( <code>alert()</code>s will fire, etc ), but the code itself does not appear to be detecting that no radio buttons are selected, and the form continues to be submitted.</p> <p>Here's the HTML form:</p> <pre><code>&lt;form action="score.php" id="answer"&gt; &lt;div style="margin-top: 0pt;" class="ans"&gt; &lt;input type="hidden" value="127" name="question_id"/&gt; &lt;input type="hidden" value="f5043d5122cec6eb981c9a2fc7a0a653" name="user_id"/&gt; &lt;input type="hidden" value="36" name="question_key"/&gt; &lt;input type="hidden" value="37" name="next_key"/&gt; &lt;ul id="answers"&gt; &lt;li&gt; &lt;label&gt; &lt;input type="radio" value="464" name="answer_id"/&gt; mod_rewrite &lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;label&gt; &lt;input type="radio" value="465" name="answer_id"/&gt; XML Site Map &lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;label&gt; &lt;input type="radio" value="466" name="answer_id"/&gt; Tiny URL &lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;label&gt; &lt;input type="radio" value="467" name="answer_id"/&gt; both a) and b) &lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;label&gt; &lt;input type="radio" value="468" name="answer_id"/&gt; a), b) and c) can all be used to make it easier for Google to index your dynamically generated URLs. &lt;/label&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;input type="submit" value="Submit Answer and Get Result" id="answer_submission"/&gt; &lt;/form&gt; </code></pre>
 

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