[英]jQuery on enter key submit form
我构建了一个简单的表单,每当您按ENTER时都会提交。 它工作正常,但是每当我尝试在其中运行.post
时,按Enter键而不是提交时,textarea中的文本都会中断。
我的意思是打破
textarea中的文本是
| 破
这很好
// Submit comment on ENTER key pressed
$("textarea[name=comment_body]").keypress(function(event) {
if (event.which == 13) {
alert(comment_body);
}
});
这不是
// Submit comment on ENTER key pressed
$("textarea[name=comment_body]").keypress(function(event) {
if (event.which == 13) {
var comment_body = $(this).val();
$.post("comments.php", {
comment_body: comment_body
}
alert(comment_body);
}
});
如何在其中使用jQuery .post而不失败?
您应该这样进行:
$("textarea[name=comment_body]").keypress(function(event) {
if (event.which == 13) {
var comment_body = $(this).val();
$.post("comments.php", {cb: comment_body}, function(data){
alert(data.cb);
}, "json");
});
在我看来,你所发布的数据{comment_body:remove_comment}
到comments.php
通过$.post()
的jQuery的方法,但你有没有使用回调函数在里面。
无论您要发布到comment.php
什么内容,回调函数中传递的data
参数中都可用。
post的语法是:$ .post(URL,data,callback);
$.post("comments.php",
{
comment_body: remove_comment
},
function(result){
alert(result.comment_body);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.