1
我迫切需要關於如何修改以下代碼的建議,以便我可以將它們用於同一頁面上的單獨迭代。下面的按鈕代碼是+ & - 圖標here。此刻,他們發生衝突,我仍然需要添加&刪除整個迭代框。有人請幫助我jquery添加和刪除輸入
這裏是我的腳本代碼:
$('#btnAdd').click(function() {
var count = $('.clonedIteration').length;
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
//add parent so that the adding is only specific to those within the class
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' + count);
//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');
嘿克羅諾斯,你用我的心意!這簡直太神奇了!謝謝! – 2011-01-25 00:54:00