2011-01-24 96 views
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'); 

回答

1

这里是的jsfiddle一个例子:http://jsfiddle.net/LetZh/1/ 您的代码是更复杂一点。我没有实现你所拥有的一切。

编辑

http://jsfiddle.net/LetZh/2/我带来了一些更正js代码。

+0

嘿克罗诺斯,你用我的心意!这简直太神奇了!谢谢! – 2011-01-25 00:54:00