开发者

How do I "catch" multiple forms with different form ids with jQuery?

Multiple forms are placed on a single page and are submitted via jquery. The code that submits the form is simple:

$("form").submit(function() {
});

The issue I am having is having a select group of forms run with this code. I have tried the following with success:

$("#form1,#form2,#form3").submit(function() {
});
开发者_如何学Go

However, is it possible select multiple forms with a single id/identifier?

Additionally, when using multiple ids to select form1, form2, and form3, what data is submitted? I have a hidden field located in form1 but it seems as if that data is sent when form2 is submitted. How can I correct this?

Thank you in advance.


<form id="form1" class="formClass"></form>
<form id="form2" class="formClass"></form>
<form id="form3" class="formClass"></form>
<form id="form4" class="formClass"></form>


$(".formClass").submit();


That first piece of code is a JavaScript event that is called on submit. It doesn't actually submit the form (omitting the function will, however).

You can select multiple forms using any valid jQuery selector. The easiest method will be to give them a class you can refer to easily.


I did a quick test in a jsFiddle. The script these forms point to is:

<?php
var_dump($_POST);

error_log(var_export($_POST, true));

The error log output was:

[error] [client 192.168.9.1] array (\n  'field1' => '1',\n), referer: http://fiddle.jshell.net/_display/

I take this to mean that a) only one form submitted, and b) it was the first form in the DOM. Someone correct me if part of my methodology is wrong.

Essentially, what this will mean is that you'll want to interrupt any submit events and combine all of the form data into one set of parameters before submitting.


Please check here in jsfiddle. I hope it helps or at least will give you an idea. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜