我如何“抓住”使用 jQuery 实现具有不同表单 ID 的多个表单?
多个表单放置在单个页面上并通过 jquery 提交。提交表单的代码很简单:
$("form").submit(function() {
});
我遇到的问题是使用此代码运行一组选定的表单。我已成功尝试以下操作:
$("#form1,#form2,#form3").submit(function() {
});
但是,是否可以使用单个 id/标识符选择多个表单?
另外,使用多个id选择form1、form2、form3时,提交什么数据呢?我在 form1 中有一个隐藏字段,但似乎在提交 form2 时发送了该数据。我该如何纠正这个问题?
先感谢您。
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() {
});
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一段代码是在提交时调用的 JavaScript 事件。它实际上并不提交表单(但是省略该函数将提交)。
您可以使用任何有效的 jQuery 选择器选择多个表单。最简单的方法是为他们提供一个您可以轻松参考的课程。
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.
我在 jsFiddle 中进行了快速测试。这些表单指向的脚本是:
错误日志输出是:
我认为这意味着 a) 仅提交了一个表单,b) 它是 DOM 中的第一个表单。如果我的方法有部分错误,有人会纠正我。
本质上,这意味着您需要中断任何
submit
事件,并在提交之前将所有表单数据合并到一组参数中。I did a quick test in a jsFiddle. The script these forms point to is:
The error log output was:
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.请在 jsfiddle 中查看。我希望它能对您有所帮助,或者至少能给您一个想法。 :)
Please check here in jsfiddle. I hope it helps or at least will give you an idea. :)