当我按下enterAction时,我得到了奇怪的反应,所以当我打开萤火虫时,它告诉我我得到了一个帖子回复和得到回复(它总是没有发生但是当我离开页面闲置几分钟时我会说5或10它是随机的一种)
HTML代码
<form id="post-form" method="post" action="/v2/TheNet/ajax/ajax.php" onsubmit="return false">
<textarea id="post_message_id" placeholder="Write something..." name="publish">
</textarea>
<input type="hidden" value="post_message" name="action">
</form>
JS代码
$(function () {
shiftEnterNewLine('post_message_id', enterAction);
$('#post-form').on('submit', function (e) {
e.preventDefault();
});
});
function enterAction() {
var xhr = new XMLHttpRequest();
var form = new FormData(document.getElementById("post-form"));
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log(xhr.responseText);
$('#post_message_id').val('');
}
};
//initiate request
xhr.open('post', '/v2/ajax/TheNet/ajax.php', true);
xhr.send(form);
}
function shiftEnterNewLine(id, action) {
$('#' + id).keydown(function (e) {
//enter
if (e.keyCode == 13 && !e.shiftKey) {
e.preventDefault();
action();
// console.log("a");
// // shift
}
});
}
回复图片&gt;
解答: 我认为jquery很好,但我的PHP代码重定向到该文件夹
答案 0 :(得分:1)
在代码表单中添加 onsubmit =“return false”:
<form id="post-form" method="post" action="/v2/TheNet/ajax/ajax.php" onsubmit="return false">
<textarea id="post_message_id" placeholder="Write something..." name="publish">
</textarea>
<input type="hidden" value="post_message" name="action">
</form>
答案 1 :(得分:0)
您应该编写此代码。这将解决您的问题
<form id="post-form" method="post" action="/v2/TheNet/ajax/ajax.php" onsubmit="return false">
<textarea id="post_message_id" placeholder="Write something..." name="publish">
</textarea>
<input type="hidden" value="post_message" name="action">
</form>
答案 2 :(得分:0)
很抱歉打扰我的jquery代码是正确的但我的php代码在令牌过期时重定向到header('Localtion: .')
我只需要修改它。