将 FORM 元素更改为其他元素或删除它,同时保留所有子元素

发布于 2024-09-26 03:34:06 字数 1127 浏览 0 评论 0原文

我已将 Asp.net MVC 与 Sharepoint 站点集成。效果很好。但我也使用默认的 Sharepoint 母版页(即 ~masterurl/default.master )。问题是这个特定的母版页将整个页面包装在 form 元素中,而所有 Sharepoint 页面只是普通的 Asp.net WebForm 页面。

我的 MVC 内容应该有自己的 form 和我想要提交到服务器的输入元素。 Ajax 调用显然没有那么有问题。如果保留原始 form 元素,我将无法使用 $("form").serializeArray() 。但尽管如此,正常的回发还是有问题的,因为提交事件有 WebForm 功能,并且它们会向服务器发送太多数据。

我将使用正常回发以及 Ajax 回发。主要问题是正常回发。

所以我有两种可能性,两者都应该在 $(document).ready() 上运行:

  1. 删除表单元素并保持所有其他内容不变。在 jQuery 中,这可能意味着在 form 元素前面加上 div,将其内容移动到此 div,然后删除 form
  2. 直接将 form 元素更改为 div。就浏览器处理而言,这可能会更快,但我不知道该怎么做。

因此,如果有人可以为 2. 制定一个解决方案,并就这两种可能的解决方案提供一些意见:应该采取哪一种以及为什么。

编辑

还有第三种可能性。我可以根据需要创建输入元素,但将它们包含在 div 元素中。当用户想要提交我的表单div无论如何都不是表单)时,我可以:

  1. 动态调用$("div.form").serializeArray()
  2. 创建一个表单元素
  3. ,用序列化值填充它
  4. ,将表单附加到正文,
  5. 提交它。

看起来很乏味,但它可以工作。前两个解决方案似乎仍然更简单。

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():

  1. Delete form element and keep all other content as is. In jQuery it would probably mean to prepend form element with a div, move its content to this div and then delete form.
  2. Directly change form element to div. 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:

  1. dynamically call $("div.form").serializeArray()
  2. create a form element
  3. populate it with serialized values
  4. append form to body
  5. submit it.

Seems tedious but it could work. Still first two solutions seem simpler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-10-03 03:34:06

如果您通过 $.ajax() 提交,我' d 只需直接使用元素的 .serialize() 即可,如下所示:

$.ajax({
  url: "Path/Action",
  type: "post",
  data: $("#someContainer :input").serialize(),
  success: function(data) {
    //do something
  }
});

或更短的 $.post() 版本:

$.post("Path/Action", $("#someContainer :input").serialize(), function(data) {
  //do something
});

这不会不需要任何

生成或其他技巧,只需一个普通的 jQuery AJAX post(或 GET,无论需要什么)。

If you're submitting via $.ajax() I'd just go with .serialize() of the elements directly, like this:

$.ajax({
  url: "Path/Action",
  type: "post",
  data: $("#someContainer :input").serialize(),
  success: function(data) {
    //do something
  }
});

Or the shorter $.post() version:

$.post("Path/Action", $("#someContainer :input").serialize(), function(data) {
  //do something
});

This doesn't require any <form> generation or other trickery, just a normal jQuery AJAX post (or GET, whatever's needed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文