这么基本的大纲...
我从数据库中提取了“帖子”并显示如下:
<div class="blogtest">
<form action="process/updatepost.php" class="updatepost" method="post">
<input type="button" class='.$editenabled.' value="Edit">
<input type="submit" class="saveupdatebutton" value="Save">
<input type="hidden" class="postid" name="postid" value="'.$postID.'">
<div class="text">
<div class="buildtext">'.$text.'</div>
<div class="editor"><textarea name="ckeditor"id="ckeditor" class="ckeditor">'.$text.'</textarea></div>
</div>
</form>
</div>
单击编辑按钮后,buildtext 类将隐藏并显示 ckeditor。编辑和保存按钮也是如此。
单击保存时,将进行 ajax 调用,然后更新数据。这工作得很好......但是,如果该页面上只有 1 篇博客文章,它只会工作得很好。
以下是供参考的ajax:
$(document).ready(function(){
$(".updatepost").submit(function(){
var $targetForm = $(this);
$targetForm.find(".error").remove();
$targetForm.find(".success").remove();
// If there is anything wrong with
// validation we set the check to false
var check = true;
// Get the value of the blog update post
var $ckEditor = $targetForm.find('.ckeditor'),
blogpost = $ckEditor.val();
// Validation
if (blogpost == '') {
check = false;
$ckEditor.after('<div class="error">Text Is Required</div>');
}
// ... goes after Validation
if (check == true) {
$.ajax({
type: "POST",
url: "process/updatepost.php",
data: $targetForm.serialize(),
dataType: "json",
success: function(response){
if (response.databaseSuccess)
$targetForm.find(".error").remove();
else
$ckEditor.after('<div class="error">Something went wrong!</div>');
}
});
}
return false;
});
});
因此,如果页面上有 2 篇博文,并且我编辑了第 2 篇(最后一篇)博文,则点击保存后该博文会正确更新。
但是,如果我编辑任何其他内容,则需要两次提交才能发送数据。
我检查了萤火虫,它显示在第一次点击时发送旧值,然后在第二次发送新值。
我哪里错了?
最终(一旦工作)帖子将在 ajax 调用成功时刷新,但现在用户只需单击一次保存显然至关重要。
谢谢你的帮助!需要更多代码并将其发布在此处。
克雷格:)
编辑:使 ckeditor 只是一个普通的文本区域后,它工作正常。必须是 ckeditor 没有更新,因为我知道它实际上不能作为 textarea 工作。也许生病不得不使用另一个富编辑器......