简体   繁体   English

在不同的div中使用ajax打开和关闭html标签

[英]Open and close html tag with ajax in different div

I need to make this.我需要做这个。 I need to open with ajax a form tag in a div, but i need to close this form in other div below, like this:我需要用ajax在div中打开一个表单标签,但我需要在下面的其他div中关闭这个表单,如下所示:

opening form(header div)打开表格(标题div)

  $('#header').html("<form action = '/company/create' method = 'POST'>"+
                     "<div>"+
                     //code
                     "</div>");

closing form(body div)结束形式(正文 div)

 $('body').html("<div>"+
               //code
               "</div>"+
               "</form>");

i tried two methods, append() and html().我尝试了两种方法,append() 和 html()。

You can't do this.你不能这样做。 You have to build the entire html string before using jquery's html function.在使用 jquery 的html函数之前,您必须构建整个 html 字符串。 This snippet shows what happens when you try to generate html with just an open tag-- jQuery automatically adds the end tags for you.这段代码展示了当你试图用一个开放标签生成 html 时会发生什么——jQuery 会自动为你添加结束标签。 The snippet will then ask you to continue, where it appends the result of html that has just an ending </form> tag.然后,该代码段将要求您继续,并在其中附加仅具有结束</form>标记的 html 的结果。 The closing tags that don't have matching opening tags are thrown away.没有匹配的开始标签的结束标签将被丢弃。

 // borrowed from http://stackoverflow.com/questions/3614212/jquery-get-html-of-a-whole-element jQuery.fn.outerHTML = function() { return jQuery('<div />').append(this.eq(0).clone()).html(); }; var x = $('<form>'); //display what jquery built for us when we only supply an open tag. $('#result1_1').text(x.outerHTML()); // display what happens when we try to append additional html with a closing tag for the first bit. var next = $('<div><p>stuff</p></form>') $('#result1_2').text(x.outerHTML() + next.outerHTML());
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> Result 1.1: display what jquery built for us when we only supply an open tag. <div id="result1_1"> </div> <br /><br /> Result 1.2:display what happens when we try to append additional html with a closing tag for the first bit. <div id="result1_2"> </div>

This snippet should be how you do it, building the entire html string at once.这个片段应该是你怎么做的,一次构建整个 html 字符串。

 jQuery.fn.outerHTML = function() { return jQuery('<div />').append(this.eq(0).clone()).html(); }; var x = "<form><div>"; x += "<p>stuff</p></div>"; x += "</form>"; // now that the entire html string is built (at least, entire innards with open and closing tags), // we can let jquery create the elements. $('#result2').text($(x).outerHTML()); $('#sampleHtml').html(x);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> Text output: <div id="result2"> </div> <br /><br /> Html Sample: <div id="sampleHtml"> </div>

I could resolve this problem using .appendTo of jquery.我可以使用 jquery 的.appendTo解决这个问题。 I created a form and I put inside it two div, as you can see in the following example.我创建了一个表单,并在其中放置了两个 div,如下例所示。

<div id = "box">
 <div id = "header"></div>
 <div id = "body"></div>
<div>

using .appendTo I did this使用.appendTo我做了这个

-firts: I created the form inside the div box -firts:我在 div 框内创建了表单

$('#box').append('<form id = "box-form"></form>');

-second: I put the header and body div into created form -second:我将标题和正文 div 放入创建的表单中

$('#header').appendTo('#box-form');
$('#body').appendTo('#box-form');

This is the final result这是最终的结果

<div id = "box">
 <form id = 'box-form'>
     <div id = "header"></div>
     <div id = "body"></div>
 </form>
<div>

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

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