2010-08-12 105 views
0

我有一塊克隆三個字段的代碼,但是當克隆這三個字段時,它也克隆了它裏面輸入的文本,有沒有辦法清除裏面的內容當它被克隆時,它的領域是什麼?克隆一個字段而不克隆文本以及

$(document).ready(function() { 
    $('#btnAdd').click(function() { 
     var num  = $('.clonedSection').length; 
     var newNum = new Number(num + 1); 

     var newSection = $('#clonedSection' + num).clone().attr('id', 'clonedSection' + newNum); 

     newSection.children(':first').children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum); 
     newSection.children(':nth-child(2)').children(':first').attr('id', 'age' + newNum).attr('name', 'age' + newNum); 
     newSection.children(':nth-child(3)').children(':first').attr('id', 'school' + newNum).attr('name', 'school' + newNum); 

     $('.clonedSection').last().append(newSection); 
     $('.clonedSection').last().val(ping); 

     $('#btnDel').attr('disabled',''); 

     if (newNum == 2) 
      $('#btnAdd').attr('disabled','disabled'); 
    }); 

    $('#btnDel').click(function() { 
     var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have 
     $('#clonedSection' + 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'); 
    }); 

    $('#btnDel').attr('disabled','disabled'); 
}); 

Thanx提前!

+0

爲什麼你要問的確切同樣的問題兩次? http://stackoverflow.com/questions/3467457/cloning-a-field-with-text-in-it-clones-text-as-well – 2010-08-12 12:27:28

+0

沒理由,是個意外! :/ – Odyss3us 2010-08-13 10:42:04

回答

1

可以清除輸入的內容是這樣的:

newSection.find(':input').val('');