【发布时间】:2011-01-23 16:00:29
【问题描述】:
我设法在网上找到允许我删除和添加文本框的代码。但是,我正在尝试修改代码以适应我在同一页面上的单独迭代中添加和删除文本框的情况,这些代码不允许我这样做..这是我添加和删除的代码:
$('#btnAdd').click(function() {
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
// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
// manipulate the name/id values of the input inside the new element
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
//prepend table code
$('#input' + num).append('<tr><td colspan="2"><label>');
// insert the new element after the last "duplicatable" input field
$('#input' + num).after(newElem);
//prepend table code
$('#input' + newNum).append('</label></td></tr>');
// enable the "remove" button
$('#btnDel').attr('disabled','');
// business rule: you can only add XXX times
if (newNum == 5)
$('#btnAdd').attr('disabled','disabled');
return false
});
$('#btnDel').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
$('#input' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').attr('disabled','');
// if only one element remains, disable the "remove" button
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
return false
});
$('#btnDel').attr('disabled','disabled');
这是我的意思的link: '+' 和 '-' 符号是代码的用途,但它们不能在不同的场合使用......无论我打算增加多少次迭代,我能做些什么来让它们在同一个 html 页面上可用还是减少?
【问题讨论】: