js复制克隆

原文链接: https://www.mk2048.com/blog/blog.php?id=h0c1k102jj1j&title=js%E5%A4%8D%E5%88%B6%E5%85%8B%E9%9A%86


$(function() {//开始加载
updateIndex()
})

function add(){

var str = $(".tr_wqxx").first().clone(true)//复制该类的第一个为模板

$("#tb_wpxx").append(str)//把模板添加到对应id的table中

str.find("input").val('');//把模板中value值清空

str.find("input").eq(4).attr("class","bottom_button_qx");//添加删除按钮

updateIndex()//执行方法把List中name 数组下标更新
}

function updateIndex() {//把数组下标更新确保不会重复

$(".tr_wqxx").each(function(index, e) {//获取选中数组

var str = $(e)

str.find("td:eq(0) input").attr("name","wpxxList[" index "].items");

str.find("td:eq(1) input").attr("name","wpxxList[" index "].number");

str.find("td:eq(2) input").attr("name","wpxxList[" index "].unit");

str.find("td:eq(3) input").attr("name","wpxxList[" index "].bak1");

str.find("td:eq(5) input").attr("name","wpxxList[" index "].id");
})
}

function delWpxx(obj){//删除方法
var _len = $("#tb_wpxx tr").length
  if(_len==3){//删除到最后一个时不许删除,留下一个当做克隆模板
    return
  }
var curr = $(obj).parent().parent();
curr.remove();
updateIndex()
}


更多专业前端知识,请上 【猿2048】www.mk2048.com

猜你喜欢

转载自blog.csdn.net/whiteGay/article/details/102766258
今日推荐