2010-06-25 106 views
1

請快速提問。你如何處理JQuery克隆對象?JQuery進程克隆對象

簡單的例子:

<div class="hello"> 
<select name="products[]"> 
<?php foreach ($pageposts as $post): 
<option value="'.$post->ID.'">'.$post->post_title.'</option> 
endforeach;?> 
</select> 
<input type="text" name="try[]"> 
<br/> 
</div> 
<form> 
<div id="goodbye"></div> 
<input type="button" id="rp" value="add"> 
</form> 

這個jQuery(下面)創建類 '你好' 的.goodbye DIV這是一種形式的內部內的 「克隆/秒」。

$j=jQuery.noConflict(); 
$j(document).ready(function() { 
$j('#rp').click(function(){ 
var str = $j(this).parent('form').serialize(); 
$j('.hello').clone().removeClass('hello').appendTo('#goodbye'); 
alert(str); 
}); 
}); 

我需要做的是過程,「克隆」選擇放置在表單內/輸入。警報在那裏,所以我可以看到什麼時候(我不知道)值得通過。

在此先感謝

回答

0

的問題是這裏的排序,這兩條線被顛倒:

var str = $j(this).parent('form').serialize(); 
$j('.hello').clone().removeClass('hello').appendTo('#goodbye'); 

你序列化<form>添加到它,只要將它周圍像這樣:

$j=jQuery.noConflict(); 
$j(document).ready(function() { 
    $j('#rp').click(function(){ 
     $j('.hello').clone().removeClass('hello').appendTo('#goodbye'); 
     var str = $j(this).parent('form').serialize(); 
     alert(str); 
    }); 
});​ 

You can see a working demo here