Javascript e、 键代码在jquery中不起作用
嗨,我正在使用jquery和php制作评论系统。问题是e.keycode不起作用…它直接进入php页面,但我只希望它只发送数据并保留在页面中…(我使用Handlebar进行模板制作)以下是我的代码: 标记:Javascript e、 键代码在jquery中不起作用,javascript,jquery,Javascript,Jquery,嗨,我正在使用jquery和php制作评论系统。问题是e.keycode不起作用…它直接进入php页面,但我只希望它只发送数据并保留在页面中…(我使用Handlebar进行模板制作)以下是我的代码: 标记: <form id="insert_comments" action="insert_comments.php" method="POST"> //the comment <input type="text" name="comment" id="comment" place
<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标准化了所有DOM 0、1和2处理程序,因为它们都有很大的不同
keyCode
将在某些浏览器上工作,但在其他浏览器上不工作。相反,只需使用jQuery标准化跨浏览器的e.which
。您的表单已经在键*down*处提交,因此使用键*up*时为时已晚。将keyup替换为keydown,它可以工作:
您真的应该检查代码缩进;)jQuery事件为您提供了一个规范化属性,可用于检测按下的键,因此使用
if(e.which==13)代替keyCode
{
您确实意识到发布的代码中存在多个语法错误。您是否打开控制台并检查错误。控制台中没有错误。您还必须查看如何正确注释代码。这简直太可怕了。它仍然不起作用…!!!我预先准备了一个警报('enter key is presed'))在ajax调用仍然不起作用之前!!…你的意思是什么?在我上面发布的fiddle中,你将停留在当前页面上,并且不会被重定向到表单的目标…?fiddle正在工作,但是当我在文件中使用它时,它会再次重定向到该页面insert_comments.phpDid你是否修改了任何内容?例如,你不应该修改任何内容rt()任何事情,这都可能导致问题。我明白了:D,因为这是一个表单,当按下enter键时,chrome正在提交它,但e。preventDefault()
应该阻止chrome提交表单。我想这是你的问题?;)
//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"
});
// 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"
});