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

Javascript 在发送更新的数据之前,需要提交两次表单,javascript,php,jquery,ajax,blogs,Javascript,Php,Jquery,Ajax,Blogs,所以基本轮廓 我从数据库中提取了“帖子”,并显示如下: <div class="blogtest"> <form action="process/updatepost.php" class="updatepost" method="post"> <input type="button" class='.$editenabled.' value="Edit"> <input type="submit" class=

所以基本轮廓

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

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

“.$text。”
“.$text。”
单击edit按钮后,buildtext类将隐藏,并显示ckeditor。编辑和保存按钮也是如此

单击save时,会进行ajax调用,然后更新数据。这个很好用。。。然而,只有当该页面上只有一篇博客文章时,它才能非常好地工作

以下是供参考的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;
});
$(文档).ready(函数(){
$(“.updatepost”).submit(函数(){
var$targetForm=$(此项);
$targetForm.find(“.error”).remove();
$targetForm.find(“.success”).remove();
//如果有什么问题
//验证我们将检查设置为false
var检查=真;
//获取博客更新文章的值
var$ckEditor=$targetForm.find('.ckEditor'),
blogpost=$ckEditor.val();
//验证
如果(blogpost==''){
检查=错误;
$ckEditor.after('Text Is Required');
}
//…需要验证
如果(检查==true){
$.ajax({
类型:“POST”,
url:“process/updatepost.php”,
数据:$targetForm.serialize(),
数据类型:“json”,
成功:功能(响应){
if(response.databaseSuccess)
$targetForm.find(“.error”).remove();
其他的
$ckEditor.after('Something出了问题!');
}
});
}
返回false;
});
}))

因此,如果页面上有2篇博客文章,并且我编辑了第2篇(最后一篇)文章,单击“保存”后,文章将正确更新

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

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

我哪里做错了

最终(一旦工作)在ajax调用成功后,帖子将被刷新,但现在用户只需单击save一次显然是至关重要的

谢谢你的帮助!需要更多的代码,我会在这里发布

克雷格:)


编辑:将ckeditor设置为普通文本区域后,它可以正常工作。必须是ckeditor不更新,因为我知道它作为文本区本身不起作用。也许我必须使用另一个富编辑器…

据我所知,CKEditor很可能使用自己的onsubmit事件将数据发送回textarea。这意味着这两个事件可能会按相反的顺序触发,首先从代码中检索旧文本,然后才更新textarea

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

var editor_data = CKEDITOR.instances.yourInstance.getData(); 
另外,您是否将jQuery适配器与CKEditor一起使用


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

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

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

var editor_data = CKEDITOR.instances.yourInstance.getData(); 
另外,您是否将jQuery适配器与CKEditor一起使用


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

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

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

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

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

我看到代码的这一部分四处浮动,但似乎从来没有工作过/我无法理解它。这里是我使用的标准值:var$ckEditor=$targetForm.find('ckEditor'),blogpost=$ckEditor.val();您是否能够从中创建一些可以帮助我理解它的内容?如何创建CKEditor实例?它们都有不同的自定义ID还是都称为“ckeditor”?都称为ckeditor,因为它们在foreachah中从数据库中回显,这将是一个问题-元素ID必须是页面唯一的,因此它们都必须有不同的ID(类名可以是相同的-ckeditor,但ID必须不同),感谢您的帮助,在玩游戏并使用一些数学知识给每个编辑器提供了自己的id号之后,所有的代码都可以工作:)我看到了代码的一部分,但似乎从来没有工作过/我无法理解它。这里是我使用的标准值:var$ckEditor=$targetForm.find('ckEditor'),blogpost=$ckEditor.val();您是否能够从中创建一些可以帮助我理解它的内容?如何创建CKEditor实例?它们都有不同的自定义ID还是都称为“ckeditor”?都称为ckeditor,因为它们在foreachah中从数据库中回显,这将是一个问题-元素ID必须是页面唯一的,因此它们都必须有不同的ID(类名可以是相同的-ckeditor,但ID必须不同),感谢您的帮助,在玩游戏和使用一些数学给每个编辑器它自己的id号后,所有工作:)你的