jquery提交多个表单 [英] jquery submit multiple forms

查看:138
本文介绍了jquery提交多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有4个表单。根据用户的选择,表格1必须以表格2,3或4提交。

jQuery的 .submit()只能执行一种形式。



发布表单的ajax不是一个选项



我只能想到将表单1的内容添加到另一种形式如前提交后一种形式。

有没有其他方法可以做到这一点?



更新:

4个表单而不是
只有1的原因是我验证每个
表单是独立存在的,因为
3表单中的2个表单是隐藏的。


解决方案

如果一个表单依赖于另外两种形式,这听起来像是它们真的是同一个表单的一部分,而后端应该只是找出那个大型表单的哪些部分(具有不同的数据集,但是对于一种形式)。在类似的说明中,最好不要依赖javascript,所以这样做可以为您提供一个自动更强大的解决方案。



更新

strong>如果您有多个表单只是共享数据(就像他们可以更改form3需要的form1和form2上的数据一样),那么它会更加稳定,特别是在验证方面,可以将这些共享的东西放在隐藏的元素中,形式分开。让用户输入将要提交和使用的数据,然后从视图中隐藏数据并不是最佳选择,因为他们可以更容易地输入错误(他们无法轻松查看,他们可能不知道它会全部提交)。使用<对于共享信息,input type =hidden/> s可以缓解这种情况,因为它们不会被 able 意外更改。 (或者将它存储在会话中或任何你想要的内容中,关键是,防止它们改变你将要依赖的信息,并且不能轻易发现错误)。

I have 4 forms on a page. Form 1 must be submitted with either form 2,3 or 4 depending on user selection.

jQuery's .submit() can only do one form.

ajax posting the forms is not an option

i can only think of adding the content of Form 1 to the other form as before submitting the latter form.

is there any other way to do it?

UPDATE:

The reason for the 4 forms instead of just 1 is that I am validating each form independently, since the 2 of the 3 forms are hidden from view.

解决方案

If one form relies on two other forms, it sounds like they're really part of the same form, and the backend should just figure out which parts of that one mega-form (with different sets of data, but for one form) that are needed. On a similar note, it's best not to rely on javascript alone anyway, so doing it this way provides you with an automatically more robust solution.

Update If you have multiple forms that are just sharing data (like they can change data on form1 and form2 that form3 needs), it would be more stable, especially in light of validation, to put those shared things in hidden elements and leave the forms separate. Having users enter data that will be submitted and used and then hiding it from view isn't optimal, since they can more easily make entry mistakes (they can't review as easily and they may not understand it will all be submitted). Using < input type="hidden" />s for shared info alleviates this because they won't be able to accidentally change it. (Or store it in the session or whatever you'd like. Point is, prevent them from changing info that you're going to rely upon and they can't easily spot mistakes in.)

这篇关于jquery提交多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆