简体   繁体   English

将FORM元素更改为其他内容或在保留所有子元素的同时将其删除

[英]Change FORM element to something else or delete it while keeping all child elements

I've integrated Asp.net MVC with Sharepoint site. 我已经将Asp.net MVC与Sharepoint网站集成在一起。 It works great. 效果很好。 But I'm using default Sharepoint master page as well ( ~masterurl/default.master namely). 但是我也使用默认的Sharepoint母版页(即~masterurl/default.master )。 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. 问题是该特定母版页的整个页面都包装在一个form元素中,而所有Sharepoint页面只是普通的Asp.net WebForm页面。

My MVC content should have it's own form and input elements that I'd like to submit to the server. 我的MVC内容应具有我想提交给服务器的自己的form和输入元素。 Ajax calls are obviously not that problematic. Ajax调用显然没有问题。 I'm not able to use $("form").serializeArray() if I keep the original form element. 如果保留原始form元素,则无法使用$("form").serializeArray() But nonetheless normal postbacks are problematic, since there's WebForm functionality on submit events and they would send way too much data to server. 但是,尽管如此,正常的回发还是有问题的,因为提交事件具有WebForm功能,它们会向服务器发送过多的数据。

I will be using normal postbacks as well as Ajax postbacks. 我将使用普通的回发以及Ajax的回发。 The main problem being normal postbacks. 主要问题是正常的回发。

So I have two possibilities that both should run on $(document).ready() : 所以我有两种可能性都应该在$(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 . 在jQuery中,它可能意味着要在form元素前添加一个div ,将其内容移至该div,然后删除form
  2. Directly change form element to div . 直接将form元素更改为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. 因此,如果有人可以为2.制定解决方案,并就这两种可能的解决方案提供一些意见:应该执行哪一种,为什么?

Edit 编辑

There's a third possibility as well. 还有第三种可能性。 I could just create my input elements as neede but containe them inside a div element. 我可以根据需要创建输入元素,但可以将它们包含在div元素中。 When user would want to submit my form ( div is not a form anyway) I could: 当用户想要提交我的表单时 (无论如何div都不是表单),我可以:

  1. dynamically call $("div.form").serializeArray() 动态调用$("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. 仍然前两种解决方案似乎更简单。

If you're submitting via $.ajax() I'd just go with .serialize() of the elements directly, like this: 如果您通过$.ajax()提交,我将直接使用元素的.serialize() ,如下所示:

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

Or the shorter $.post() version: 或更短的$.post()版本:

$.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). 这不需要任何<form>生成或其他技巧,只需一个普通的jQuery AJAX帖子(或GET ,无论需要什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM