首页 > 解决方案 > 如何在 ajax 加载 textarea 后重新初始化 TinyMCE

问题描述

我正在尝试将 tinyMCE 重新绑定到使用 ajax 动态生成的 textarea。我的tinymce看起来像这样:

tinyMCE.init({
    selector : "textarea",
    plugins: "emoticons link",  
    menubar: false,
    toolbar: 'undo redo | bold italic underline | fontsizeselect | link | emoticons',    
    height: 240,
    force_br_newlines : true,
    force_p_newlines : false,
    forced_root_block : '',
    paste_as_text: true,

});

我用于发送数据的 ajax:

$('body').on('submit', '.comment_form', function (e) {
    e.preventDefault();
    tinyMCE.triggerSave(); // save TinyMCE instances before sending data
    ....
    $.ajax({
        url: 'comment.php',
        type: 'POST',
        data: {             
                comment_name:comment_name,              
                comment:comment,                                                                    
                },
        beforeSend: function(){
            tinyMCE.remove(); // remove tiny mce form textarea
        },      

        success: function(data){

            $('.result').html(data);            
            $('.modal').modal('hide'); // close modal   

            $(".comments-body").load(" .comments-body > *"); // reload div comments-body from comments  


        },
        complete:function(){
            tinyMCE.init(); // rebind tinymce to textarea       
        }
    });

});



带有类的 divcomments-body有几种带有 textareas 的表单,并在成功后重新加载。但是在重新加载后,tinymce 不再绑定!

我知道我必须使用tinyMCE.remove();并且tinyMCE.init();我在我的 ajax 中尝试过beforeSendcomplete但没有结果。有人知道我在做什么错吗?我正在使用 tinymce 5 的免费 API

标签: javascriptjqueryajaxtinymce-5

解决方案


推荐阅读