2016-01-25 39 views
2

我无法追加页面似乎只是刷新在那里,因为我想查看它无需刷新页面无法附加jQuery的

的index.php

<div class="view_comment"> 
     <b>username :</b> <?php echo $comment_comment;?> 
</div> 
<div id="comment_type_area" class="comment_type_area"> 
     <form method="POST"> 
      <input type="text" class="comment" post_id="<?php echo $shared_id2; ?>" id="text_comment" placeholder="Write a Comment"></input> 
      <input type="submit" id="post_button" ></input> 
     </form> 
     </div> 

的jquery.js

$(document).ready(function(){ 

     $('.comment').keydown(function (e){ 
      if(e.keyCode == 13){ 
       var post_id = $(this).attr('post_id'); 
       var comment = $(this).val(); 
       $.post('/comment.php',{ post_id: post_id, comment:comment}); 
       $('.comment').val(''); 
/*i am guessing the problem starts from here and onwards*/ 
       $(this).parent().children('.comments').append("<div class='view_comment'><b>Username :</b>" + comment +"</div>"); 
      } 
     }); 

    }); 
+1

你确信没有JavaScript错误投掷到浏览器的控制台? –

+0

表单提交... – epascarello

+0

以及表单确实提交,因为我可以在我的数据库中看到它,我也能够从我的数据库 – jake123

回答

0

错误是在jQuery的岗位我被追加它..children('comments').append这是错了,它应该是这样的

$(document).ready(function(){ 

    $('.comment').keydown(function (e){ 
     if(e.keyCode == 13){ 
      e.preventDefault(); 
      var post_id = $(this).attr('post_id'); 
      var comment = $(this).val(); 
      $.post('/comment.php',{ post_id: post_id, comment:comment}); 
      $('.comment').val(''); 
      $(this).parent().append("<div class='view_comment'><b>Username :</b>" + comment +"</div>"); 
     } 
    }); 

}); 
6

您应该添加e.preventDefault()以防止提交,因为当您点击输入button形式的领域,将自动提交表单:

if(e.keyCode == 13){ 
    e.preventDefault(); 

    var post_id = $(this).attr('post_id'); 
    var comment = $(this).val(); 
    ... 
} 

注:输入是自我封闭的标签之一,所以它应该是<input type="submit" id="post_button" />

希望这会有所帮助。

+0

'e.preventDefault(); '帮助我停止重新加载页面,但我无法追加 – jake123

+0

请尝试将完整的HTML添加到OP,因为我找不到任何带有类“comments”的元素。 –

+0

没有元素作为类'评论' – jake123