当用户在 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> <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>