开发者

Adding data dynamically to a HTML page

In a htmlpage using jQuery or JavaScript how can the following be achieved?

User types in a question in a textarea and press on enter button, then the same question should be displayed dynamically in the page below the textarea and the user can enter as 开发者_JAVA百科many questions.

And when the user is done the page is submitted through a submit button.

Can you give a small hint of how this can be done?


Try this to get started :

<!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>


Use the innerHTML property of a div to add the questions to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜