2010-08-12 95 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('');