Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should <strong><em>always</em></strong> use an HTML <code>&lt;form&gt;</code> element - even (and especially) if you're using AJAX. It will be more usable, accessible, and will make your life as a developer easier. Do a Google search for "ajax graceful degradation" or "accessible ajax" if you don't believe me; it's actually pretty easy.</p> <p>For example, here's an AJAX form that degrades gracefully using <a href="http://jquery.com/" rel="nofollow">jQuery</a>, the greatest JavaScript library on earth:</p> <pre><code>&lt;form method="post" action="ajax/test.html" name="ajax-form" id="ajax-form"&gt; &lt;fieldset&gt; &lt;legend&gt;Form name&lt;/legend&gt; &lt;p&gt;&lt;label for="username"&gt;Username: &lt;input type="text" name="username" id="username" value="" /&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt;&lt;label for="password"&gt;Password: &lt;input type="password" name="password" id="password" value="" /&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="option-a"&gt;Option A: &lt;input type="radio" name="options" id="option-a" value="a" /&gt;&lt;/label&gt;&lt;br/&gt; &lt;label for="option-b"&gt;Option B: &lt;input type="radio" name="options" id="option-b" value="b" /&gt;&lt;/label&gt;&lt;br/&gt; &lt;label for="option-c"&gt;Option C: &lt;input type="radio" name="options" id="option-c" value="c" /&gt;&lt;/label&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="select-box"&gt;Select Box: &lt;select name="select-box" id="select-box" size="1"&gt; &lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt; &lt;option value="3"&gt;3&lt;/option&gt; &lt;/select&gt; &lt;/label&gt; &lt;/p&gt; &lt;p class="buttons"&gt;&lt;button type="submit"&gt;Submit&lt;/button&gt; &lt;button type="reset"&gt;Reset&lt;/button&gt;&lt;/p&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; $().ready(function() { var $ajaxForm = $('#ajax-form'); // Create container to store ajax result $ajaxForm.find('.buttons').after($('&lt;p class="result"/&gt;')); // Bind event handler $ajaxForm.bind('submit', function(event, data) { // Simple ajax POST request // See http://api.jquery.com/jQuery.post/ $.post($ajaxForm.attr('action'), 'ajax=1&amp;' + $ajaxForm.serialize(), function(data) { $('.result').html(data); }); // Disable default form submit behavior return false; }); // Bind click handler to override "reset" behavior... // Although this really isn't necessary if you're using the HTML &lt;form&gt; element, which you should be... $ajaxForm.find('button:reset').bind('click', function(event, data) { var $els = $ajaxForm.find('input, textarea, select'); $els.filter('input:text, input:password, textarea').val(''); $els.filter('input:radio, input:checkbox').attr('checked', false); $els.filter('select').attr('selectedIndex', ''); return false; }); }); &lt;/script&gt; &lt;style type="text/css"&gt; fieldset { border: 0; padding: 0; margin: 0; } fieldset legend { display: none; } &lt;/style&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