我在我的网站上的帖子下方有评论框,我希望人们能够通过简单地按返回键来提交评论。注释也是通过 ajax 插入的。
我的代码是:
对于 AJAX 帖子并捕获回车键:
$('textarea#scat').bind('keypress', function(e) {
if(e.keyCode==13){
var myClass = $(this).attr("class");
var comment = $("textarea." + myClass).val();
if (comment == "") {
return false;
}
if (!$.trim($("textarea." + myClass).val())) {
return false;
}
var cid = $("input.c_" + myClass).val();
var itemid = $("input.i_" + myClass).val();
var type = $("input.t_" + myClass).val();
var top = $("input.l_" + myClass).val();
var dataString = 'comment='+ comment + '&cid=' + cid + '&itemid=' + itemid + '&type=' + type;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "addcomment.php",
data: dataString,
success: function() {
$('#c').load('ajax/querylc.php?oid=' + myClass);
$("textarea." + myClass).val('');
}
});
return false;
};
});
还有我的评论框代码:
<div id='statuscomadd' class="<?php echo $sid; ?>" style='text-align:center; padding-top:2px; margin-left:12.5px; border-left:1px #a3a3a3 solid; border-right:1px #a3a3a3 solid; border-bottom:1px #a3a3a3 solid; height:40px; width:420px; vertical-align:middle;'>
<img id="scati<?php echo $sid; ?>" src='<?php if ($dp == null) { echo 'img/unknown_user.jpg'; } else { echo 'pf/' . $uid . '/' . $dp; } ?>' style='height:36.5px; margin-right:5px; margin-bottom:6px;'>
<form action='addcomment.php' method='post' id='ac' style='display:inline; border:0px; margin: 0 0 0 0; padding: 0 0 0 0;'>
<textarea id='scat' style='outline: none; height:30px; width:315px; font-family:Arial; border:0px; resize:none; margin-bottom:5px; border:1px solid #C9C9C9; display:inline;' name='comment' class='<?php echo $sid; ?>'></textarea>
<input type='hidden' class="t_<?php echo $sid; ?>" name='type' value='status' />
<input type='hidden' class="i_<?php echo $sid; ?>" name='itemid' value='<?php echo $sid; ?>'/>
<input type='hidden' class="c_<?php echo $sid; ?>" name='cid' value='<?php echo $uid ?>' />
</form>
</div>
如果我使用表单提交按钮,则该帖子有效,但如果我使用返回键则无效,当前发生的只是它添加了一个新行,我只想在 shift+enter 时发生。
任何帮助将非常感激!