将 FORM 元素更改为其他元素或删除它,同时保留所有子元素
我已将 Asp.net MVC 与 Sharepoint 站点集成。效果很好。但我也使用默认的 Sharepoint 母版页(即 ~masterurl/default.master
)。问题是这个特定的母版页将整个页面包装在 form
元素中,而所有 Sharepoint 页面只是普通的 Asp.net WebForm 页面。
我的 MVC 内容应该有自己的 form
和我想要提交到服务器的输入元素。 Ajax 调用显然没有那么有问题。如果保留原始 form
元素,我将无法使用 $("form").serializeArray()
。但尽管如此,正常的回发还是有问题的,因为提交事件有 WebForm 功能,并且它们会向服务器发送太多数据。
我将使用正常回发以及 Ajax 回发。主要问题是正常回发。
所以我有两种可能性,两者都应该在 $(document).ready()
上运行:
- 删除表单元素并保持所有其他内容不变。在 jQuery 中,这可能意味着在
form
元素前面加上div
,将其内容移动到此 div,然后删除form
。 - 直接将
form
元素更改为div
。就浏览器处理而言,这可能会更快,但我不知道该怎么做。
因此,如果有人可以为 2. 制定一个解决方案,并就这两种可能的解决方案提供一些意见:应该采取哪一种以及为什么。
编辑
还有第三种可能性。我可以根据需要创建输入元素,但将它们包含在 div
元素中。当用户想要提交我的表单(div
无论如何都不是表单)时,我可以:
- 动态调用
$("div.form").serializeArray()
- 创建一个表单元素
- ,用序列化值填充它
- ,将表单附加到正文,
- 提交它。
看起来很乏味,但它可以工作。前两个解决方案似乎仍然更简单。
I've integrated Asp.net MVC with Sharepoint site. It works great. But I'm using default Sharepoint master page as well (~masterurl/default.master
namely). the problem is that this particular master page has the whole page wrapped in a form
element while all Sharepoint pages are just normal Asp.net WebForm pages.
My MVC content should have it's own form
and input elements that I'd like to submit to the server. Ajax calls are obviously not that problematic. I'm not able to use $("form").serializeArray()
if I keep the original form
element. But nonetheless normal postbacks are problematic, since there's WebForm functionality on submit events and they would send way too much data to server.
I will be using normal postbacks as well as Ajax postbacks. The main problem being normal postbacks.
So I have two possibilities that both should run on $(document).ready()
:
- Delete form element and keep all other content as is. In jQuery it would probably mean to prepend
form
element with adiv
, move its content to this div and then deleteform
. - Directly change
form
element todiv
. This would probably be faster in terms of browser processing, but I don't know how to do it.
So if anyone can elaborate a solution for 2. and provide some input on these two possible solutions: which one should be done and why.
Edit
There's a third possibility as well. I could just create my input elements as neede but containe them inside a div
element. When user would want to submit my form (div
is not a form anyway) I could:
- dynamically call
$("div.form").serializeArray()
- create a form element
- populate it with serialized values
- append form to body
- submit it.
Seems tedious but it could work. Still first two solutions seem simpler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您通过
$.ajax()
提交,我' d 只需直接使用元素的.serialize()
即可,如下所示:或更短的
$.post()
版本:这不会不需要任何
If you're submitting via
$.ajax()
I'd just go with.serialize()
of the elements directly, like this:Or the shorter
$.post()
version:This doesn't require any
<form>
generation or other trickery, just a normal jQuery AJAX post (orGET
, whatever's needed).