如何停止提交表单div中的所有字段?
我将表单分为两个部分:sec1
和 sec2
。每个部分都是名为 sec1Div
和 sec2Div
的 div 的一部分。根据某些选择,div 之一被隐藏。但问题是隐藏部分中的字段仍然被提交。请建议一种方法,以便在提交时不会提交 div 中的所有字段。
I have divided form in to two sections: sec1
and sec2
. Each section is part of a div named as sec1Div
and sec2Div
. Based upon some selection one of div is hidden. But the problem is that still fields in hidden section are submitted. Please suggest a way so that all of fields in a div are not submitted on submit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法可以做到这一点。您可以将函数挂钩到表单
submit
的事件,也可以删除表单的name
属性隐藏 div 内的字段。您还可以通过设置disabled="disabled"
来禁用字段。如果您使用 jQuery,您可以执行这些示例。
要禁用隐藏 div 中的所有字段,您可以执行以下操作:
并且,适当的显示 div 函数:
请提醒,这只是一个代码示例。但你可以从中得到这个想法。
There are several ways to do that. You can hook a function to the form
submit
's event, or you can remove thename
attributes of the fields inside the hidden div. You can also disable the fields, by settingdisabled="disabled"
.If you are using jQuery, you can do those examples.
To disable all fields in the hidden div, you can do something like:
And, the appropriate show div function:
Please remind that this is just a code example. But you can take the idea from that.
发生这种情况的原因是元素仍在
form
元素内。使用 CSS 隐藏div
不会改变这一点 - 它们仍然存在于 DOM 中。最简单的方法可能是向每个
div
添加一个隐藏的input
字段,该字段可用于识别服务器端您应该处理哪个字段。然后,您可以简单地忽略隐藏表单中的数据。如果您确实必须阻止数据被发布,这有点混乱,但您可以将隐藏的
div
的内容移到form
元素之外,这样字段就不会'不提交。如果您想再次显示div
,则需要将字段移回原位。根据 CSS 的复杂程度,这可能会在某些浏览器中导致问题,因此我建议使用我的第一个建议。The reason this is happening is because the elements are still within the
form
element. Hiding adiv
using CSS won't change this - they're still present in the DOM.It would likely be easiest to add a hidden
input
field to eachdiv
that can be used to identify server side which one you should be processing. You can then simply ignore the data from the hidden form.If you really must stop the data from being posted, it's a little messy but you could move the hidden
div
's contents outside of theform
element so that the fields won't be submitted. If you wanted to display thediv
again, you'd then need to move the fields back in. Depending on how complex your CSS is, this could cause problems in some browsers, so I'd advise using my first suggestion.