I have the following:
<script type="text/javascript">
function CancelFormButton(button) {
$(button.form).submit();
}
</script>
<form onsubmit="alert('here');">
<input type="button" value="Cancel" onClick="CancelFormButton(this);" />
</form>
When I click the "Cancel" button, the onsubmit from the form tag is not triggered.
This line instead submits the form successfully: $(button.form).submit();
but skips the alert('here');
within the onsubmit in the form tag.
Is this correct or am I doing something wrong?
By the way, in this case, I want this functionality, but I'm just wondering if I'm going to run into a problem in a browser where the onsubmit is triggered.
I suppose it's reasonable to want this behavior in some cases, but I would argue that code not triggering a form submission should (always) be the default behavior. Suppose you want to catch a form's submission with
and then do some validation on the input. If code could trigger submit handlers, you could never include
inside this handler because it would result in an infinite loop (the SAME handler would be called, prevent the submission, validate, and resubmit). You need to be able to manually submit a form inside a submit handler without triggering the same handler.
I realize this doesn't ANSWER the question directly, but there are lots of other answers on this page about how this functionality can be hacked, and I felt that this needed to be pointed out (and it's too long for a comment).
I found this question serval years ago.
recently I tried to "rewrite" the submit method. below is my code
}
It did in IE,but failed in other browsers for the same reason as "cletus"
Try to trigger() event in your function: