The code is stuck in a loop when I try to catch a form submission. The purpose is to replace a value of the form before it goes out.
$('form').submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: 'convert.asp',
data: $('form').serialize(),
success: function(response){
$('input[name="field1"]').val(response);
$('form').submit();
}
});
return false;
});
Does anyone have any ideas?
UPDATE: I originally had it bound to a button click event and it was working but I wanted to preserve the [enter] key element of the submit button. Seeing that the code is kind of illogical, would catching a keypress be a better idea?
To submit a form using jQuery click event, you need to detect the submit event. Let’s submit a form using click event and without using click event. You can try to run the following code to learn how to submit a form using jQuery click event −
This method is a shortcut for .on ( "submit", handler ) in the first variation, and .trigger ( "submit" ) in the third. The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to <form> elements.
@mblase75 is right here, there is no need for the inner submit. You've bound the submit event of the form to the function within so the second call calls the first again and so on. @Christoph What about it?
This simply works out of the box because the confirm () method blocks the event loop, therefore the form is not submitted until the user clicks one of the two choices. Now, let’s say that we want to use a nice-looking jQuery plugin, much more appealing to the users, to display the dialog. But when we try to emulate the previous example:
I assume your ajax is an attempt to validate the form before its eventual submission. In that case just unbind the validation function before submitting the form.
success: function(response){
$('input[name="field1"]').val(response);
// Add unbind to remove validations
$('form').unbind().submit();
}
In the success you trigger another submit...
$('form').submit(function(e){
e.preventDefault(); // redundant, you return false in the end. <<<===
$.ajax({
type: "POST",
url: 'convert.asp',
data: $('form').serialize(),
success: function(response){
$('input[name="field1"]').val(response);
$('form').submit(); // <=== delete this! <<<<=================
}
});
return false;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With