dtx3006 2013-09-26 19:00
浏览 40

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

so basic outline...

I have 'posts' pulled from the database and displayed like so:

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

Once the edit button is clicked, the buildtext class hides and the ckeditor is shown. Same for the edit and save button.

When save is clicked, a ajax call is made and then the data is updated. This works perfectly fine... however it only works perfectly fine if there is only 1 blog post on that page.

Here is the ajax for reference:

$(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;
});

});

So, if there is 2 blog posts on the page and i edit the 2nd (last) post, on clicking save the post is updated correctly.

However if i edit any other then it takes two submits for the data to be sent.

I checked on firebug and it shows that on the first click, the old value is sent, and then on the 2nd the new one.

Where am i going wrong?

Eventually (once working) the post will be refreshed on the success of the ajax call but for now its obviously vital that the user only has to click save once.

Thanks for any help! Any more code needed and ill post it here.

Craig :)

EDIT: After making ckeditor just a normal textarea it works fine. Must be ckeditor not updating as i know it doesnt litrally work as a textarea as such. Maybe ill have to use another rich editor...

  • 写回答

2条回答 默认 最新

  • dongqing4070 2013-09-26 19:13
    关注

    As far as I can see from the integration guide, CKEditor most probably uses its own onsubmit event to actually send data back to the textarea. This would mean that those 2 events might fire up reversed order, first retrieving the old text from your code and only then updating the textarea.

    You can always try and retrieve CKEditor's content using the following syntax:

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

    Also, are you using the jQuery adapter with CKEditor?

    EDIT: the problem seems to be having same IDs on multiple textareas, all being called "ckeditor". This would result in unexpected behaviour across browsers, since ID must be always unique to a page.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分