Jquery get form values, multiple forms

I have several forms on the page, and I need to get the values ​​of the form that were submitted, but without specifying the form.

$(document).ready( function (){
 $('form').submit( function(){
       return submitForm()
 });

});

and in the .js file submitForm () should identify the identifier of the form that is submitted. How can I do that?

I get the values ​​of the form [0], not the ones clicked

+3
source share
2 answers

Not sure what it does submitForm(), but I assume that you are somehow using thisit.

If yes, try:

 $('form').submit( function(){
       return submitForm.call( this );
 });

The execution of this method should send the context of the form on which the call was clicked submitForm().

, submitForm() this jQuery $(this).


EDIT:

, : http://jsfiddle.net/pXwTE/

+4

submitForm :

$j(document).ready( function (){
    $j('form').submit( function(){
       return submitForm($j(this).attr('id'));
    });

});

, , , :

$j(document).ready( function (){
    $j('form').submit( function(){
       alert($j(this).serialize());
    });
});

:

$j(document).ready( function (){
    $j('form').submit( function(){
        $j(':input', this).each(function(){
           alert($j(this).val());
       });
    });
});

: http://jsfiddle.net/2HRjF/1/

+1

Source: https://habr.com/ru/post/1755785/


All Articles