【发布时间】:2014-03-14 16:09:45
【问题描述】:
我的代码遇到了错误。我正在克隆一个 div,以便用户可以添加多个客户。
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
var newElem = $('#divInput' + num).clone().attr('id', 'divInput' + newNum); // create the new element via clone(), and manipulate it's ID using newNum value
// clear input value for cloned items and do not remove text for del button.
newElem.find('input:not(.DeleteBtn),textarea').val('');
//newElem.find('input[type="submit"]
// Replace clone num with incremental num.
newElem.find(':input').each(function () {
$(this).attr('id', $(this).attr('id').replace(/\d+/, newNum));
$(this).attr('name', $(this).attr('name').replace(/\d+/, newNum));
});
// insert the new element after the last "duplicatable" input field
$('#divInput' + num).after(newElem);
我提供了一个删除按钮来删除行,并且我正在使用按钮的类名来执行 click 函数。
$(".DeleteBtn").click(function () {
alert(".DeleteBtn Click Function - " + $(this).attr('id'));
var DelBtnNum = $(this).attr('id');
DelBtnNum = DelBtnNum[DelBtnNum.length - 1];
$('#divInput' + DelBtnNum).remove();
});
我可以删除第一个(原始)输入行,但不会删除任何其他客户行。
我有一个运行的代码演示,位于:http://jsfiddle.net/crjunk/FB4BZ/2/
为什么克隆的项目不会触发 .DeleteBtn.click 函数?
【问题讨论】:
-
您是否尝试过:
.clone(true)符合 DOC 中的建议? jsfiddle.net/FB4BZ/3
标签: javascript jquery html