3

jsfiddle を参照してください: http://jsfiddle.net/bjCz3/

html

<table> <tr class="copyMe"> <td><input type="text" name="test" /></td> </tr> </table> <a id="clickMe" href="#">Click Me</a>

jquery

  $('#clickMe').click(function(event){
   event.preventDefault();

   var tr = $('.copyMe:last');
   var newTr = tr.clone();
   newTr.appendTo(tr.parent());
  }); 

入力にテキストを入力してクリックすると、行 (入力を含む) が複製されて挿入され、入力したデータが含まれます。

clone() の API は次のように述べています。

The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes. For performance reasons, the dynamic state of form elements (e.g., user data typed into input, and textarea or user selections made to a select) is not copied to the cloned elements. The clone operation sets these fields to their default values as specified in the HTML.

http://api.jquery.com/clone/

So why does my input have the value filled in, and more important, how can I prevent it? I want them to be empty/default like the documentation implies. I tried specifying both arguments as false even though they default to that, and it still copies it.

4

3 に答える 3

4

チェックされていない、またはページの読み込み時に値が設定されている入力を使用している場合。(または、設定されているデフォルトが必要です)...ユーザーが触れる前に行をキャッシュします。次に、その保存された行を複製して、必要に応じて追加します

/* store row on page load*/
 var tr = $('.copyMe:last').clone();
 $('#clickMe').click(function(event){
   event.preventDefault();

  var newTr = tr.clone();....

})
于 2013-11-05T21:35:52.453 に答える