您好我正在使用jquery和php制作评论系统。问题是e.keycode不起作用...它直接进入php页面,但我只是想让它只是发送数据并保留在页面中...(我正在使用把手进行模板化)以下是我的代码:
标记:
<form id="insert_comments" action="insert_comments.php" method="POST">
//the comment
<input type="text" name="comment" id="comment" placeholder="add your comment.." />
//the post_id where the comment is passed
<input type="hidden" id="post_id" name="post_id" value="{{post_id}}"/>
//this is the $_GET['user'] variable here I'm displaying a number
//so I'm using get_user function to get the full name using the number
<input type="hidden" name="to_user" id="to_user" value="<?php echo get_user('roll_no',"$get_user",'fullname','users'); ?>"/>
</form>
jquery:
//the object named as comment
var comment = {
//the function which handles all operations which accepts config methods as parameters
init: function(config){
this.config = config;
//trigger the events methos in this object
this.events();
},
//the event function
events: function(){
//the area of the comment text box
this.config.textarea.keyup(function(e){
//if the key is enter key
if(e.keyCode==13 ){
//here we are preventing default of that area so it won't jump to other page but it's not working!!
e.preventDefault();
//this var is created for using the this word
var self = comment;
//the ajax call
$.ajax({
url: self.config.url,
type: "POST",
data: self.config.form.serialize(),
success: function(data){
//on success just console.logging the result...
console.log(data);
}
}); //end of ajax
} //end of if statement
}); //end of the event keyup
} //end of this method
};//end of this object
comment.init({
form: $("#insert_comments"),\
textarea: $("#comment"),
url: "insert_comments.php"
});
答案 0 :(得分:0)
jQuery标准化所有DOM 0,1和2处理程序,因为它们都完全不同。 keyCode
适用于某些浏览器,但不适用于其他浏览器。相反,只需使用e.which
,jQuery标准化跨浏览器。
答案 1 :(得分:0)
您的表单已经在密钥*向下*提交,因此使用密钥* up *时为时已晚。通过keydown替换keyup并且它可以工作:http://jsfiddle.net/4LsrE/
// the object named as comment
var comment = {
// the function which handles all operations which accepts config methods as parameters
init: function (config) {
this.config = config;
// trigger the events methos in this object
this.events();
},
// the event function
events: function () {
// the area of the comment text box
this.config.textarea.keydown(function (e) {
// if the key is enter key
if (e.which === 13) {
// here we are preventing default of that area so it won't jump to other page but it's not working!!
e.preventDefault();
// this var is created for using the this word
var self = comment;
// the ajax call
$.ajax({
url: self.config.url,
type: "POST",
data: self.config.form.serialize(),
success: function (data) {
// on success just console.logging the result...
console.log(data);
}
}); // end of ajax
} // end of if statement
}); // end of the event keyup
} // end of this method
}; // end of comment
comment.init({
form: $("#insert_comments"),
textarea: $("#comment"),
url: "insert_comments.php"
});