0

In my form, I have bound a function to form submission:

        $(document).ready(function() {
            // Bind the submission to the form
            $('form').bind('submit', onsubmit_action);
        });

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:

        function onsubmit_action(event) {
            if (initiator == "form.buttons.finish") {
                $('form').unbind('submit', onsubmit_action);
                event.preventDefault();
                sendRequest();
            } else {
                this.submit();
            }
        }

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:

<form action="/carry_on" method="post"></form>

the sendRequest() method & its handler:

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
});
};

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:

$('form').on('submit', function() {
 post(this.action, this.serialize(), null, "script");
});

or something. (I'm guessing on the above syntax) Any helpful ideas are appreciated.


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.

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?

2 Answers 2

0

I think .data('events') lists all bound events on an element set. Perhaps you can get that, find the submit event handle, and cache it for later rebinding?

Found it while Googling around here: http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/

1
  • First, that page is great. Second, using .data('events') I was able to determine that something else was wrecking my day, because I could see that the default action was STILL bound. Thanks for this link and taking the time to read.
    – Spanky
    Jan 25, 2013 at 21:34
0

The code here binds and event handler to the link, there is no unbind() but a simple switch and a jQuery data(), my_process() should set 'status' to 'normal'.

$(document).ready(function(){

    $('a').bind('click', function(e){
        var status = $(this).data('status');
        switch(status){
            case 'processing':
                e.preventDefault();
                $(this).data('status', 'processing');
                break;
            case 'normal':
                break;
            default: /* First click */
                e.preventDefault();
                my_process($(this));  
        } /* end of switch */
    });/* end of bind() */
});/* end of ready() */

Example: http://jsbin.com/etuqig/4/edit

The logic of the event handler

Using the data() function, it detects the link state and acts accordingly:

  1. If it's triggered for the first time:
    • It fires our custom function
    • It does a prevent default
  2. If it's triggered while our custom function is working
    • It does a prevent default
  3. It's triggered after our function finished
    • It does nothing, and the default behaviour happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.