如何在jquery中克隆
我正在使用此代码进行克隆如何在jquery中克隆,jquery,Jquery,我正在使用此代码进行克隆 当我点击克隆按钮时,我想修改此代码,它会一次又一次地克隆克隆和为每个新生成的动态div删除按钮 当我第一次单击“克隆”按钮时,它使用相同的id克隆相同的div,然后开始递增 你可以在这里找到一个工作版本 $(文档).ready(函数(){ //jQuery(this.parent(“.clonedInput”) var regex=/^(.*)(\d)+$/i; 变量克隆索引=$(“.clonedInput”).length; $(“button.clone”).li
$(文档).ready(函数(){
//jQuery(this.parent(“.clonedInput”)
var regex=/^(.*)(\d)+$/i;
变量克隆索引=$(“.clonedInput”).length;
$(“button.clone”).live(“单击”,函数(){
$(this).parents(“.clonedInput”).clone().appendTo(“body”).attr(“id”,“clonedInput”+cloneIndex)
.find(“*”).each(函数(){
var id=this.id | |“”;
var name=this.name | |“”;
var match=id.match(正则表达式)| |[];
var matchname=name.match(正则表达式)| |[];
如果(match.length==3){
this.id=匹配[1]+(克隆索引);
}
if(matchname.length==3){
this.name=匹配[1]+(克隆索引);
}
});
克隆索引++;
});
$(“button.remove”).live(“单击”,函数(){
$(this.parents(“.clonedInput”).remove();
});
});
克隆
去除
您需要在克隆输入长度中添加1
var cloneIndex = $(".clonedInput").length + 1;
// -----^^^^ here
您需要在克隆输入长度中添加1
var cloneIndex = $(".clonedInput").length + 1;
// -----^^^^ here
在声明
克隆索引时,您需要如下声明它
var cloneIndex = $(".clonedInput").length+1;
在声明克隆索引时,您需要如下声明它
var cloneIndex = $(".clonedInput").length+1;
您可以将其简化为
$(document).ready(function() {
// jQuery(this).parent(".clonedInput")
var regex = /^(.*)(\d)+$/i;
var cloneIndex = $(".clonedInput").length + 1;
$(document).on("click", 'button.clone', function() {
$(this).closest(".clonedInput").clone().appendTo("body").attr("id",
"clonedInput" + cloneIndex).find("[id], [name]").each(
function() {
this.id = this.id.replace(/\d+$/, cloneIndex);
this.name = this.name.replace(/\d+$/, cloneIndex);
});
cloneIndex++;
});
$("button.remove").live("click", function() {
$(this).parents(".clonedInput").remove();
});
});
演示:您可以将其简化为
$(document).ready(function() {
// jQuery(this).parent(".clonedInput")
var regex = /^(.*)(\d)+$/i;
var cloneIndex = $(".clonedInput").length + 1;
$(document).on("click", 'button.clone', function() {
$(this).closest(".clonedInput").clone().appendTo("body").attr("id",
"clonedInput" + cloneIndex).find("[id], [name]").each(
function() {
this.id = this.id.replace(/\d+$/, cloneIndex);
this.name = this.name.replace(/\d+$/, cloneIndex);
});
cloneIndex++;
});
$("button.remove").live("click", function() {
$(this).parents(".clonedInput").remove();
});
});
演示:谢谢回复大家,但我只想克隆和删除按钮一次,不总是。而且它出现在身体上的每件事我需要下一步再下一步。谢谢回复大家,但我只想克隆和删除按钮一次不总是。它出现在身体上的每件事我需要下一步再下一步再下一步。