morsor
morsor

Reputation: 1313

jQuery: Reseting form action after submit

I'd like to be able to submit a form to an alternative URL (which is not a problem) - and after submit set the form action to what it originally was.

$(".submitForExport").click(function() {

    // Saves current form action
    var originalAction = $(this).parents('form').attr('action');

    // Sets form action to export URL, submits and resets form action to previous action
    $(this).parents('form').attr('action', $('#alternativeUrl').val());
    $(this).parents('form').submit();
    $(this).parents('form').attr('action', originalAction);
});

However, the above code submits the form to the original action. It seems as if the submit action is triggered last?

If the last line is commented out, the form submits to the alternative URL as expected - but the form action is now off course set to the alternative URL.

Is there a way I post-submit can set the action to what is originally was? Or is there a better way to do this? Perhaps Ajax?

Upvotes: 0

Views: 512

Answers (1)

sp00m
sp00m

Reputation: 48807

Try to add a return false; at the end of your function. I guess your click event is catched on a submit button. So, your form is submitted twice: one with your manually triggered .submit() method, and another one at the end of the function, because of the type="submit" of the button.

Upvotes: 1

Related Questions