2013-06-18 25 views
-1
<script type="text/javascript" > 
$(document).ready(function() 
{ 
$(".comment").click(function(){ 

var element = $(this); 
var id = element.attr("post_id"); 

$("#"+id).html('<form action="test.php" method="post"><textarea name="body" id="body_'+id+'"></textarea></form>'); 

$("#body_"+id).focus(); 

return false; 

}); 
}); 

textarea的jQuery的不能持有我在

<a href="#" post_id="17" class="comment">Open</a> 
<div id="17"></div> 

爲什麼每次當我在textarea的輸入內容,然後點擊的.comment鏈接時,textarea的值變爲空白鍵入,我應該如何保持什麼我鍵入不會被刪除?

回答

0

你覆蓋你與該行的DIV中的任何HTML ...

$("#"+id).html('<form action="test.php" method="post"><textarea name="body" id="body_'+id+'"></textarea></form>'); 

試改成這樣:

var textArea = $("#body_"+id); 
var textAreaValue = textArea.length > 0 ? textArea.val() : ''; 
$("#"+id).html('<form action="test.php" method="post"><textarea name="body" id="body_'+id+'">'+textAreaValue+'</textarea></form>'); 
+0

太棒了....謝謝 –

0

您需要防止鏈接,否則重新加載頁面的默認行爲:

$(".comment").click(function(e){ 
    e.preventDefault(); 
    ... 
});