¿Cómo hacer algo antes de enviarlo? [cerrado]

i have a form which has a button that submits the form. I need to do something before submits happen. I tried doing onClick on that button but it happens after the submit.

I can't share the code but, generally, what should I do in jQuery or JS to handle this?

preguntado el 08 de noviembre de 11 a las 16:11

@Brad Christie if only i could accept comment as an answer. Thanks very helpful. -

I hate it when stackover closes a question. You can handle your actions using the following events that fire on the following timing just before submitting a form: 1- onmouseover 2- onmousemove 3- onmousedown 4- onfocus 5- onmouseup 6- onclick 7- onsubmit -

I appreciate this question because I ran into the same problem just recently and this question is exactly what I wanted to ask. -

funny that this question got closed because its not a real question :-? -

4 Respuestas

If you have a form as such:

<form id="myform">

You can use the following jQuery code to do something before the form is submitted:

$('#myform').submit(function() {
    // DO STUFF...
    return true; // return false to cancel form action

Respondido el 16 de Septiembre de 16 a las 22:09

I'm having troubles using this for loops that have to be completed before the submit. Any suggestions? - Smilyan

Got any code to look at? Not sure I understand the problem. - Dan Breen

Why is this wrapped in an empty function call? Shouldn't the submit function be able to bind directly to the $('#form') without the surrounding empty function? EDIT: the docs suggest that the surrounding function call is not needed. - eykanal

@eykanal Good point. I went ahead and edited the answer to remove the function wrapper after confirming that it does work fine with it removed. - Jon Schneider

@eykanal I used the "empty" function call, because that's a shorthand for jQuery's $(document).ready() function, which ensures the DOM is complete before attaching the submit event. If your JS is at the bottom of the page, it's not necessary, but if it's in the <head> section then you need it. See: stackoverflow.com/q/13062246/135108 - Dan Breen

Assuming you have a form like this:

<form id="myForm" action="foo.php" method="post">
   <input type="text" value="" />
   <input type="submit" value="submit form" />


Puede adjuntar un onsubmit-event with jQuery like this:

$('#myForm').submit(function() {
  alert('Handler for .submit() called.');
  return false;

Si return false the form won't be submitted after the function, if you return true or nothing it will submit as usual.

Consulta los documentación de jQuery para más información.

respondido 08 nov., 11:20

Puedes usar onclick to run some JavaScript or jQuery code before submitting the form like this:

<script type="text/javascript">
    beforeSubmit = function(){
        if (1 == 1){
            //your before submit logic
<input type="button" value="Click" onclick="beforeSubmit();" />

respondido 08 nov., 11:20

But this doesn't work, when the form contains e.g. an input field an the user is hitting the return key which is firing the submit event. It's usually better to attach directly to the submit event so you can ensure it gets called regardless how the user fired the submission. - cumbre

<input type="submit" value="Click" onmouseover="beforeSubmit();" /> - Ansyori

@Ansyori That's even worse! - Por

make sure the submit button is not of type "submit", make it a button. Then use the onclick event to trigger some javascript. There you can do whatever you want before you actually post your data.

respondido 08 nov., 11:20

The form can be submitted in a number of ways, such as pressing enter in certain fields. Binding to a click on one button that may not be clicked, is not a good idea. The other answers show how you bind onsubmit de los utilizando el formulario , which will fire no matter how the form is submitted. - Jason

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.