2017-12-27 670 views
0

我正在使用summernote html文本编辑器及其插入图像功能。 (当用户输入的图像,我把它发送到我的后端和与我替换用户的图像网址)JQuery序列化数据获取旧窗体值

例子:

用户输入:http://example.com/image.jpg - >上传此图片亚马逊S3 - >更新summernote文本区域的img src =“”(mydomain.com/blabla.jpg)。

以上逻辑运行良好。这里没有问题。

然后我想把这个texarea发送到我的后端php脚本的数据库的东西。

但是,当我serilize并提交表格。 Ajax发送用户的图片网址。没有替换图片网址。

图片上传

/*GET USER'S IMAGE URL AND SEND IT TO BACKEND. UPLOAD IT, THEN RAPLACE IT'*/ 
/*THIS PART IS WORKING CORRECTLY */ 
$('button[data-original-title="Picture"]').click(function(){ 
    // Set handler on Inset Image button when dialog window is opened 
    $('.modal-dialog .note-image-btn').on('click', function(e) { 
     var imageUrl = $('.modal-dialog .note-image-url').val(); 
     var currentTitle = $("#title").val().trim(); 
     if(currentTitle.length == 0){ 
      currentTitle = ""; 
     } 
     $.ajax({ 
      url: "upload.php", 
      data: "url="+encodeURIComponent(imageUrl)+"&title="+encodeURIComponent(currentTitle), 
      type: "POST", 
      dataType: 'json', 
      success: function(data) { 
       if (typeof data[0] === 'string') { 
        $('img[src="' + imageUrl + '"]').attr('src', data); 
       } else { 
        // What to do if image downloading failed 
        window.alert('oops'); 
       } 
      }, 
      error: function() { 
       /* console.log("error");*/ 
      } 
     }); 
    }); 
}); 

形式AJAX(问题是在这里我想)

/* SEND SERIALIZED FORM DATA TO BACKEND */ 
/* PROBLEM IS HERE. $("#form").serialize() CAN'T GET replaced IMAGE URL. */ 
$("#submitButton").on("click",function() { 
    $.ajax({ 
     url: "save-article.php", 
     data: $("#form").serialize(), 
     type: "POST", 
     success: function(data) { 
      if(data === "success"){ 
       $(".messageBox").html('<div class="alert alert-success ic">Thanks, you are redirecting</div>'); 
      }else if(data === "fail"){ 
       $(".messageBox").html('<div class="alert alert-danger ic">There was an error</div>'); 
      } 
     }, 
     error: function (e) { 
      console.log(e); 
     } 
    });*/ 
}); 

我怎样才能改变summernote文本区域的价值提交和序列化?

+0

HTML的外观如何?你有没有尝试用新值替换textarea的值?或者使用隐藏的表单域来存储新值? – xadhix

+0

Html部分看起来不错。我可以在HTML文本编辑器上看到替换的图像。这是正确的,img src =“”包含我的域的值。但是当我序列化并提交包含summernote textarea的表单时,在我的后端,我看不到被替换的src值。 @xadhix – hakiko

+0

_Html部分看起来不错_如果我们应该听取您的意见,可能需要一段时间才能有人对此问题进行猜测。 – RiggsFolly

回答

0

Summernote不使用初始化编辑器的元素,它将该元素中的数据复制到它自己的动态创建的接口中。您需要复制Summernotes编辑区域中的数据(您可以使用类别目标.note-editable)。如果你还没有的话,我建议你去看看Summernote的主站点,以获得以编程方式获取代码的例子。 https://summernote.org/getting-started/#get--set-code