Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: Rebind default form action after unbind?
    primarykey
    data
    text
    <p>In my form, I have bound a function to form submission:</p> <pre><code> $(document).ready(function() { // Bind the submission to the form $('form').bind('submit', onsubmit_action); }); </code></pre> <p>In onsubmit_action() I unbind the submit event so people can't click again, and call preventDefault on the event because I'm doing some AJAX nonsense:</p> <pre><code> function onsubmit_action(event) { if (initiator == "form.buttons.finish") { $('form').unbind('submit', onsubmit_action); event.preventDefault(); sendRequest(); } else { this.submit(); } } </code></pre> <p>The function sendRequest() is doing the real AJAX work here. If sendRequest fails, I simply rebind the form and all is well. But if it succeeds, I need to rebind the form, but not to the onsubmit_action() but instead to the original action of the form:</p> <pre><code>&lt;form action="/carry_on" method="post"&gt;&lt;/form&gt; </code></pre> <p>the sendRequest() method &amp; its handler:</p> <pre><code>handleResponse = function(data, code, jqXHR) { switch (data.status) { case "SUCCESS": // This doesn't work because "submit" is unbound $('#form-buttons-finish').click(); // Same with this $('form').submit() break; case "ERROR": alert('error') break; case undefined: alert('undefined'); break; default: alert("Some kind of horrible error occurred: " + data.status); break; } }; sendRequest = function() { // Poll the the server for a response $.ajax({ type: "POST", cache: "false", url: "/json/get_status", dataType: "json", data: { ref_token: token }, success: handleResponse, error: handleError }); }; </code></pre> <p>What I'm looking to do is rebind the form's submit event, but bind it to the default action of the form: /carry_on and not the onsubmit_action function. I'm thinking I need to do something like:</p> <pre><code>$('form').on('submit', function() { post(this.action, this.serialize(), null, "script"); }); </code></pre> <p>or something. (I'm guessing on the above syntax) Any helpful ideas are appreciated.</p> <hr> <p>There was something else interfering with my submit. The form is still actually bound to its default action, since I only unbound the onsubmit_action.</p> <p>Sorry for the noise. I would, however, like feedback on my last suggestion about using post(). Also, what would you do HAD the default action been unbound?</p>
    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.
    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