Javascript 使用Jquery在重复节内重复字段
我需要在repeating DIV部分中包含一个repeating TextArea控件(我创建了一个小jquery片段,它将克隆第一个TextArea并在点击按钮时将其附加到ul,因此我将其称为repeating TextArea控件)(用户将看到另一个添加按钮,单击该按钮时,该按钮应克隆该div并将该div添加到主容器中。用户可以按任意次数单击该按钮,因此我将其称为重复div) 我不知道如何完成此任务。这是详细的要求。(类似于InfoPath中重复部分中的重复字段)Javascript 使用Jquery在重复节内重复字段,javascript,jquery,html,dom,javascript-events,Javascript,Jquery,Html,Dom,Javascript Events,我需要在repeating DIV部分中包含一个repeating TextArea控件(我创建了一个小jquery片段,它将克隆第一个TextArea并在点击按钮时将其附加到ul,因此我将其称为repeating TextArea控件)(用户将看到另一个添加按钮,单击该按钮时,该按钮应克隆该div并将该div添加到主容器中。用户可以按任意次数单击该按钮,因此我将其称为重复div) 我不知道如何完成此任务。这是详细的要求。(类似于InfoPath中重复部分中的重复字段) 使用Jquery,我创建
使用Jquery,我创建了一个重复的textarea控件(点击Add按钮,TextAreas作为列表项添加)现在我将有一个div,它需要有一些文本框,还需要包括这个重复的textarea字段。ID还需要对所有内容都是唯一的。正如我上面提到的,在这个div之后会有一个按钮,当用户点击该按钮时,整个div需要被克隆,并且需要附加到主容器中有很多不同的方法可以做到这一点。我最近有一个项目,我必须做这件事。下面的代码示例: HTML 上面的代码中有几个有用的搜索函数:,。另外,我正在使用该函数复制HTML部分 同样值得注意的是,我使用
$(document.createElement(“textarea”)
document创建了新的textarea
以CSS为例
div.template {
display: none;
}
div.section {
border: 1px solid black;
}
div.section textarea {
display: block;
}
本例保持ID的唯一性,正如您在JSFIDLE中看到的那样。但是,如果这些字段发布到服务器上,则读取这些字段是对另一个问题的回答。您只需获取该重复字段并使用TextAreas对其进行迭代,然后将infoPath扩展到第七维,这正好位于框在顶部。完成后,你可以告诉我们你到底在说什么?Lol@adeneo…我想他有一个div
,其中包含一个textarea
,点击“添加”按钮添加一个新的textarea
。但是,这里有一个转折点,包含那些textarea
的div
也可以通过单击“添加”按钮来添加。因此,在重复部分(读:div
)内重复字段(读:textarea
)…我想。我真的很抱歉没有回答清楚我的问题。我会马上编辑它。Jwatts,你准确地描述了我面临的问题。谢谢。@user3532540你做了什么?非常感谢。我也在做同样的尝试(隐藏div和克隆那个家伙)但由于我糟糕的Jquery编程能力,无法使其工作。我将为您提供这段代码以满足我的要求,并在完成后将其标记为答案。再次感谢。
$(function() {
//add the click handler to add a new section
$("input.addsection").click(CreateSection);
//add the click handler for the new section
//since the buttons are added dynamically, use "on" on the "document" element
// with the selector for the button we want to watch for.
$(document).on("click", "input.addtextarea", function() {
var section = $(this).closest("div.section");
AddTextarea(section);
});
});
function CreateSection() {
var section = $("#section_template div.section").clone();
var holder = $("#container span#sholder");
//get the current total number of sections
var sectionCount = holder.find("div.section").length;
//create the section id by incrementing the section count
section.attr("id", "section" + (sectionCount + 1));
//add a textarea to the section
AddTextarea(section);
//add the new section to the document
holder.append(section);
}
function AddTextarea(section) {
var sectionID = section.attr("id");
var holder = section.find("span.taholder");
//get the current total number of textareas in this section
var taCount = holder.find("textarea").length;
//create the new textarea element
var ta = $(document.createElement("textarea"));
//create the textarea unique id
var taID = section.attr("id") + "_textarea" + (taCount + 1);
ta.attr("id", taID);
//show the id... can be removed
ta.val("ID: " + taID);
//add the textarea to the section
holder.append(ta);
}
div.template {
display: none;
}
div.section {
border: 1px solid black;
}
div.section textarea {
display: block;
}