layui 获取富文本内容

原创
2019/06/25 17:45
阅读数 952

HTML 

 <div class="layui-form-item magb0">
      <label class="layui-form-label">富文本内容</label>
      <div class="layui-input-block">
          <textarea class="layui-textarea layui-hide " id="suggCont" name="suggCont"
                lay-verify="suggCont"></textarea>
      </div>
</div>

JavaScript

<script type="text/javascript">
    layui.use(['form', 'layer', 'laytpl','layedit','upload'], function () {
        var form = layui.form
            , $ = layui.jquery;

        var layedit = layui.layedit;

        //创建一个编辑器
        var editIndex = layedit.build('suggCont', {
            height: 535,
            uploadImage: {
                url: "../../json/newsImg.json"
            }
        });

        //监听提交
        form.on('submit(roleFormFilter)', function (data) {
            loading = layer.load(2, {
                shade: [0.2, '#000'] //0.2透明度的白色背景
            });

            data.field.id = GetUrlParam("id");

            data.field.suggCont = layedit.getContent(editIndex);

            //上传富文本内容
            $.post(url, JSON.stringify(data.field), function (result) {
                layer.close(loading);
                if (result.code == 0) {
                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    });
                } else {
                    layer.msg(result.msg, {icon: 2, anim: 6, time: 1000});
                }
            });
            return false;
        });

    });



</script>
<script type="text/javascript">
    layui.use(['form', 'layer', 'laytpl','layedit','upload'], function () {
        var form = layui.form
            , $ = layui.jquery;

        //从后台获取富文本内容

        $.getJSON(url + GetUrlParam("id"), function (result) {
            if (result.code == 0) {
    
                $("#tplEditForm textarea[name=suggCont]").val(result.suggInfo.suggCont);
                
                var layedit = layui.layedit;
                //创建一个富文本编辑器
                var editIndex = layedit.build('suggCont');
                form.verify({
                    suggCont:function () {
                        layedit.sync(editIndex)
                    }
                });
                form.render();
            } else {
                layer.msg(result.msg, {icon: 5, time: 1000});
            }
        });


    });

</script>

总结:

方法一:

form.verify({
    suggCont:function () {
        layedit.sync(editIndex)
    }
});
form.render();

方法二:

 var layedit = layui.layedit;

 var editIndex = layedit.build('suggCont', {
      height: 535,
      uploadImage: {
           url: "../../json/newsImg.json"
      }
 });

data.field.suggCont = layedit.getContent(editIndex);
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部