weixin_33724570 2017-09-28 15:07 采纳率: 0%
浏览 222

将summernote文本传递给Ajax

I'm not a pro with ajax and javascript in general so I have to ask. I have a form with an editor inside (summernote editor)

<div id="summernote">summernote text</div>

Then I send form data throught this ajax function:

$( '#form-create-project' ).submit( function( e ) {
    $.ajax({
      url: 'create_project.php',
      type: 'POST',
      data: new FormData( this ),
      processData: false,
      contentType: false
    });
    e.preventDefault();
});

But I can't find a way to pass the summernote value too, I've tried with that:

var content = $('textarea[name="content"]').html($('#summernote').code());

but I don't know where to place that inside the js above. When I try to insert it the form on submit refresh the page instead of execute ajax, so I think I placed it in the wrong place.

Someone can help me?

  • 写回答

2条回答 默认 最新

  • 程序go 2017-09-28 15:22
    关注

    Can you try to escape the html code before to submit:

    For Example:

    txtText.summernote({ height: 400, lang: 'es-ES'});
    
    $.fn.htmlEscape = function () {
    
        return this.val().trim()
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
    },
    
    // I needed the opposite function today, so adding here too:
    $.fn.htmlUnescape = function () {
    
        return this.val().trim()
            .replace(/&quot;/g, '"')
            .replace(/&#39;/g, "'")
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/&amp;/g, '&');
    },
    
    function getDataForm() {
    
            var data = new FormData();
    
            if (Files.ImageSelection.getAction() === "choose") {
                data.append("ImagePath", ImagePath);
            }
            else {
                var files = Files.ImageSelection.getSelectedFile().get(0).files;
                if (files.length > 0) {
                    data.append("File", files[0]);
                }
                data.append("ImagePath", "");
            }
    
            data.append("Id", SectionId);
            data.append("Title", txtTitle.val().trim());
            data.append("Text", txtText.htmlEscape());
            data.append("TypeId", cboType.val());
    
            return data;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题