Form with jquery

0

I have several forms on a page and I am sending them with the ajax method, how can I identify which of these forms were sent? so I have my code

$('form').submit(function(e){}

But of course when you do this, you are grabbing all the forms I have.

    
asked by Avancini1 13.11.2017 в 04:52
source

3 answers

0

Assuming that you are controlling the submission of each form so that it is done exclusively by ajax, you only have to go through all the forms and send them. something like this:

$('form').each(function(elemento){
  $(elemento).submit();
});

In the method that sends the form you can check to see if the form was sent correctly and if you got a correct answer.

If you want to control and send, you can do it like this:

$('form').each(function(elemento){
  $(elemento).submit(function(){
    e.preventDefault(); //Detienes el envio normal.
    $.ajax({
            type: 'POST',
            url: 'url/url/url',
            data: $(this).serialize(),
            success: function (data) {
                console.log('Este formulario se envió correctamente');
            },
            error: function (data) {
                console.log('Este formulario obtuvo un error');
            },
        });
  });
});

Add an ID to each form dynamically, so you know which form got error and which was sent correctly. I do not put the code because I do not know how you generate the forms.

Greetings.

    
answered by 15.11.2017 / 00:13
source
1

Instead of:

$('form').submit(function(e){}

You place:

$('#IdDeUnoDeMisforms').submit(function(e){}

$('#IdDeOtroForm').submit(function(e){}
    
answered by 13.11.2017 в 05:04
-2

in the callback try with e.target you can still pass that the variable e of your callback to a console.log and check the json but in general it is with e.target to know that you are setting

    
answered by 13.11.2017 в 08:38