文本区域中输入键上的 AJAX 请求未按预期工作


AJAX request on enter key in textarea not working as expected

每当我按 Enter 键时,它似乎都不会发布它只是转到下一行。我尝试了alert选项,它确实有效,但是当我发布某些内容时,它不起作用。

$(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 });
        }
    });
});

索引.php:

<div class='comment_type_area'>
    <textarea class='comment' post_id='<?php $shared_id2; ?>' id='text_comment' placeholder='Write a Comment'></textarea>
    <button id='post_button' type='button'>Post</button> 

注释.php

 include 'sqlconnect.php';
 $post_id = $_POST['post_id'];
 $comment = $_POST['comment'];
 mysql_query("INSERT INTO `comment_system` 
 (`id`, `user_id`, `post_id`, `first_name`, `date_time`, `comment`) 
 VALUES (NULL, '1', '$post_id', '', '', '$comment')");

如何使按钮和回车键发布一些东西?我似乎想不通。请帮忙。

我能够修复它,我的错误在 index.php

索引.php

 <div id='comment_type_area' class='comment_type_area'>
            <form method='POST'>
                <input type='text' class='comment' post_id='<?php $shared_id2; ?>' id='text_comment' placeholder='Write a Comment'></input>
                <input type='submit' id='post_button' ></input> 
            </form>

我错过了方法部分,还更改了文本区域以输入

感谢大家的投入