无法附加jquery

jak*_*123 2 javascript php 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>
Run Code Online (Sandbox Code Playgroud)

的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>");
            }
        });

    });
Run Code Online (Sandbox Code Playgroud)

Zak*_*rki 6

您应该添加e.preventDefault()以防止提交,因为当您单击输入button将自动提交表单的表单字段时:

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

     var post_id = $(this).attr('post_id');
     var comment = $(this).val();
     ...
}
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助.