开发者

jquery get form values, multiple forms

开发者 https://www.devze.com 2023-01-08 01:38 出处:网络
I have multiple forms on the page and i need to get the v开发者_如何学Pythonalues of the form that is submitted, but without specifying the form.

I have multiple forms on the page and i need to get the v开发者_如何学Pythonalues of the form that is submitted, but without specifying the form.

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

});

and in the .js file submitForm() should identify the id of the form that is submitted. How can be that done?

I get the values of the form[0], not the one that is clicked


Not sure what submitForm() does, but I assume you're somehow using this in it.

If so, try:

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

Doing it this way should send the context of the form that was clicked to the submitForm() call.

So in submitForm() you can refer to the correct form using this, or in a jQuery object using $(this).


EDIT:

Here's an example of what I mean: http://jsfiddle.net/pXwTE/


You can pass the id of each form to your submitForm function like this:

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

});

to get all of the values, depending on what you need to do with them, you can use serialize like this:

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

or you can loop through all of the form's inputs like this:

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

I created a fiddle for this: http://jsfiddle.net/2HRjF/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消