在发送更新数据之前,表格需要提交两次


form needs to be submitted twice before sending updated data

所以基本的大纲...

我从数据库中提取了"帖子"并显示如下:

<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>

单击编辑按钮后,构建文本类将隐藏并显示 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 篇博客文章,并且我编辑了第二篇(最后一篇(文章,则单击保存后,帖子将正确更新。

但是,如果我编辑任何其他内容,则需要两次提交才能发送数据。

我检查了Firebug,它显示第一次单击时发送旧值,然后在第二次单击时发送新值。

我哪里出错了?

最终(一旦工作(帖子将在ajax调用成功时刷新,但现在用户只需单击保存一次显然至关重要。

感谢您的任何帮助!需要更多代码并在此处发布。

克雷格:)

编辑:在使ckeditor只是一个普通的文本区域后,它可以正常工作。必须是 ckeditor 不更新,因为我知道它不能作为文本区域工作。也许生病了,不得不使用另一个富有的编辑器...

我也有类似的问题。 只是为了分享我的经验,以防有人在这里遇到同样的问题。 CKEditor在第一次单击提交按钮时没有更新目标文本区域,因为我对文本区域进行了数据验证,幸运的是,我意识到第一次提交时没有填充文本区域。 为了克服这个问题,我添加了这段代码:

$('#accept-button').click(function (event) {
    for (var i in CKEDITOR.instances) {
        CKEDITOR.instances[i].updateElement();
    }
}

我从集成指南中看到,CKEditor很可能使用自己的onsubmit事件将数据实际发送回文本区域。这意味着这两个事件可能会触发相反的顺序,首先从代码中检索旧文本,然后才更新文本区域。

您始终可以尝试使用以下语法检索 CKEditor 的内容:

var editor_data = CKEDITOR.instances.yourInstance.getData(); 

另外,您是否将jQuery适配器与CKEditor一起使用?

编辑:问题似乎是在多个文本区域具有相同的ID,所有ID都被称为"ckeditor"。这将导致跨浏览器出现意外行为,因为 ID 对于页面必须始终是唯一的。