简体   繁体   English

使用jQuery提交时添加代码块

[英]Appending a block of code on submit with jQuery

http://jsfiddle.net/rfnslyr/xcqa5/ http://jsfiddle.net/rfnslyr/xcqa5/

The goal of this little "app", is to type in a filename, paste in an HTML document, and when you click submit, a little orange block below the Submit button appears stating the filename you entered, and the extracted CSS classes and ID's. 这个小“应用程序”的目标是键入文件名,粘贴到HTML文档中,然后单击“提交”,“提交”按钮下方会出现一个橙色小方框,说明您输入的文件名以及提取的CSS类和ID 。

Right now it returns the correct response in console when you paste in HTML code, but I want to display it in little code blocks below the submit button like in my example. 现在,当您粘贴HTML代码时,它会在控制台中返回正确的响应,但是像我的示例一样,我希望在“提交”按钮下方的小代码块中显示它。

Every time you paste in new code + new file name and hit submit, a second block like the first appears after the first block with the new filename and extract classes. 每次粘贴新代码+新文件名并单击Submit时,第二个块(如第一个块)就会出现在具有新文件名和提取类的第一个块之后。

This is the code that I want duplicated + populated with the filename/classes inserted on submit: 这是我要复制的代码,并用提交时插入的文件名/类填充:

<div id="classes">
    <div class="pageTitle">%filename%</div>
    <div class="cssClassesIDs">
        %classes%
    </div>
</div>

I'm not sure how to duplicate and append this code structure repeatedly on submit. 我不确定如何在提交时重复复制并附加此代码结构。

Here's a modified JSFiddle: 这是经过修改的JSFiddle:

http://jsfiddle.net/xcqa5/1/ http://jsfiddle.net/xcqa5/1/

var codeNameVal = $('input#codeName').val();
var newClone = $('#classes').clone();
$(newClone).find('div.pageTitle').html(codeNameVal);
$(newClone).find('div.cssClassesIDs').html(uniqueNames.join(','));
$('#container').append(newClone);
$(newClone).show();

Basically you have a hidden div that's built the way you want it. 基本上,您有一个以所需方式构建的隐藏div。 When they click submit you clone that hidden div, set the values inside to what you need, then append that div to the end of the container div. 当他们单击“提交”时,您将克隆该隐藏的div,将其内部的值设置为所需的值,然后将该div追加到容器div的末尾。 Once you do that you show your newly cloned div with show() . 完成后,将使用show()显示新克隆的div。

Jquery clone() jQuery clone()

http://api.jquery.com/clone/ http://api.jquery.com/clone/

and append() append()

https://api.jquery.com/append/ https://api.jquery.com/append/

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

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