我有以下代码,它是在“聊天框”中发布一条消息,但是当我输入时返回 false;在函数结束时,它不会提交数据,但是当我删除它时,它会按原样提交。
JS代码
function dopost() {
var request;
$("#chat").submit(function(event){
if(request) {
request.abort();
}
var $form = $(this);
var $inputs = $form.find("input, button");
var serializedData = $form.serialize();
$inputs.prop("disabled", true);
request = $.ajax({
url: "/chat.php",
type: "post",
data: serializedData
});
request.done(function(response, textStatus, jqXHR) {
console.log("Hooray, it worked!");
});
request.fail(function(jqXHR, textStatus, errorThrown){
console.error("The following error occured:" + textStatus, errorThrown);
});
request.always(function(){
$inputs.prop("disabled", false);
});
e.preventDefault();
});
return false;
}
表格
if(isset($_POST['chat'])) {
$message = mysql_real_escape_string($_POST['message']);
$time = time();
$userid = $user_class->id;
$result1 = mysql_query("INSERT INTO `newchat` (posted, posterid, message,extraid) VALUES ('" . $time . "', '0', '".$message."','" . $userid . "')") or die(mysql_error());
}
<form method='post' action='chat.php' id="chat">
<div class='chatwrap'>
<div class='chatlog'>
</div>
</div>
<div style='width:600px;margin:0 auto;padding: 5px'>
<input type='text' class='w400' name="message" value placeholder="Type message here..." /> <input type="submit" onclick="dopost();" name="chat" />
</div>
</form>
即使我将 return false 添加到 onclick 它也不会发布,任何人都可以找到解决方案吗?将不胜感激