2010-03-12 127 views
0

在使用jQuery或JavaScript一個htmlpage如何以下操作來實現?動態地將數據添加到一個HTML頁面

用戶在textarea中輸入問題並按下回車按鈕,然後在textarea下面的頁面中動態顯示相同的問題,用戶可以輸入許多問題。

而用戶完成後,通過提交按鈕提交頁面。

你能給如何可以做到這一點小提示?

+1

您是否嘗試過但寫這個代碼? – Jeremy 2010-03-12 11:33:23

回答

3

試試這個上手:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Example</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $('#textareabutton').click(function(){ 
     var q = $('#textarea').val(), 
      $p = $('<p>').html(q); 
     $('#questions').append($p); 
     }); 
     $('#submit').click(function(){ 
     var tab; 
     tab = $('#questions p').serializeArray(); 
     // now do something with $.ajax to submit the questions 
     $.post("myphp.php",tab,function(resp){ 
      // what do I do with the server's reply ??? 
     }); 
     }); 
    }); 
    </script> 
    <style type="text/css"> 
    </style> 
</head> 
<body> 
    <textarea id='textarea'></textarea> 
    <button type='button' id='textareabutton'>Add question</button> 
    <div id='questions'></div> 
    <button type='button' id='submit'>Submit questions</button> 
</body> 
</html> 
+0

謝謝....我會嘗試這個 – Hulk 2010-03-12 13:01:03