Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery AJAX submit button sometimes doesn't work
    primarykey
    data
    text
    <p>Greetings all,</p> <p>I'm using a link as an JQuery AJAX submit button, and sometimes it fires when clicked and sometimes it doesn't. The failure to fire is intermittent. It seems like it happens after the AJAX form is submitted but returns with errors which I display. </p> <p>Then when I click the submit button/link again sometimes nothing happens, and sometimes it works fine. No javascript errors show on the console. Here is what my code looks like. I would appreciate your thoughts. Do I need to be using .live()? And if so, why? I'm thinking maybe I don't understand how the binding works fully.</p> <p>Thanks, -Northk</p> <pre><code>// make the comment form submit link behave as if it's a submit button. // this code is inside $(document).ready. Corresponding HTML looks like: // &lt;div class="box round-box darker comment-message-box"&gt; // &lt;p class="comment-message"&gt;&lt;/p&gt; // &lt;/div&gt; // // &lt;a href="#" id="comment-button" class="action-button"&gt;&lt;small&gt;Submit&lt;/small&gt;&lt;/a&gt; // $(function() { $('#comment-button').click(function(e) { e.preventDefault(); // prevent the browser from "jumping" on the page when it comes back from AJAX. $('.comment-message-box').hide(); // clear any leftover errors that may be showing on the form $('#comment_form').submit(); }); }); </code></pre> <p>Sorry, here is the AJAX code. I was trying to keep my question short but probably didn't give enough code-context:</p> <pre><code>$(function() { $('#comment_form').ajaxForm({ url: 'http://www.mywebsite.com', success: function(data) { if (data.match(/&lt;title&gt;Error&lt;\/title&gt;/)) { //grab the error message var error = $(data).find('ul li:first').text(); // if they didn't enter any comment, this is the error we'll get back from the server if (error == 'The comment field is required') { $('.comment-message').replaceWith('&lt;p class="comment-message mtm mbm"&gt;Please enter a comment.&lt;/p&gt;'); } // else some other kind of error occurred else { $('.comment-message').replaceWith('&lt;p class="comment-message mtm mbm"&gt;I&amp;rsquo;m sorry! for some reason the comment form isn&amp;rsquo;t working at this time. Please &lt;a href="/contact"&gt;contact me&lt;/a&gt; for help.&lt;/p&gt;'); } } else { // else no error, fix up a success message $('.comment-message').replaceWith('&lt;p class="comment-message mtm mbm"&gt;Thanks for your comment!&lt;/p&gt;'); } // display the return message box $('.comment-message-box').fadeIn(1000); }, dataType: 'html' }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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