3

当用户在 textarea 中按下回车键时,我想调用 insert_comment() 函数:与 facebook 相同。

<form action="" method="post" enctype="multipart/form-data" name="comment_form" id="comment_form">
<tr>
<td colspan="3" align="left" valign="top"><textarea name="comment_text" id="comment_text_<?php echo $i; ?>"  class="valid Post_Description_Text" placeholder="Write a comment here..."></textarea></td>
</tr>
<tr>
<td colspan="3" align="right" valign="top"><span id="extra_small_round_loader_<?php echo $postID; ?>"></span>&nbsp;<input type="button" name="comment" value="Comment" id="comment" onclick="insert_comment(<?php echo $i; ?>);" /></td>
</tr>
</form>

这是我想在 textarea 中按 Enter 时调用的函数 insert_comment():

<script type="text/javascript">
function insert_comment(id)
{
    var comment_des         = $("#comment_text_"+id).val();
    var fk_post_id          = $("#fk_post_id_"+id).val();
    var post_user_id        = $("#post_user_id_"+id).val();
    var user_id             = $("#user_id_"+id).val();

        $.post('ajax_files/insert_comment.php', {comment_des:comment_des,user_id:user_id,fk_post_id:fk_post_id,post_user_id:post_user_id},
        function(data) 
        {
            //$("#ajaxdata").html(data);
            $("#extra_small_round_loader_"+fk_post_id).empty().html('<img src="images/extra_small_round_loader.gif">');
            $('#reload_posts').load('ajax_files/reload_posts.php');


        });
}
</script>

已解决: 好的,我找到了解决方案,我测试了它并且它的工作完美。

这是文本区域字段:

<textarea name="comment_text" id="comment_text_<?php echo $i; ?>"  class="valid Post_Description_Text" placeholder="Write a comment here..." onkeyup="enter_comment(event,<?php echo $i; ?>);"></textarea>

还有功能:

<script type="text/javascript">
function enter_comment(event,id) {
       if (event.which == 13) {
           insert_comment(id); // Call any function here: Just pass your actual Parameters to enter_comment().
       }

}
</script>
4

2 回答 2

1

尝试将 onkeyup 处理程序添加到您的文本区域:

onkeyup="if(event.keyCode==13) insert_comment(<?php echo $i; ?>);"
于 2013-08-13T05:28:27.263 回答
0
$(".Post_Description_Text").keypress(function(event) {
       if (event.which == 13) {        
            insert_comment(/((0-9)+)/.exec($(this).attr(id) )[0] );
         }

});
 You can check if the enter key is pressed usinge event.which. Keycode for enter is 13. The regex is just to extract the post id from the id of textbox
于 2013-08-13T05:30:30.477 回答