Ckeditor不加载通过ajax在弹出对话框中生成的元素

ckeditor not loading on element generated via ajax on popup dialog?

本文关键字:对话框 元素 加载 ajax Ckeditor      更新时间:2023-09-26

我使用自定义表单和生成表单元素与ajax调用,但textarea没有加载ckeditor。这个表单加载在弹出对话框上。下面是我的代码:

ajax code:
    jQuery.ajax({
    type: "POST",
    url: "reg_arz_ajax2.php",
    data: "book="+book_arzyabi,
    dataType : "html",
    success: function(response){
        $('#resp').html(response);
                    ckeditor.replace('fname'); 
        $("#fname").ckeditor();
    },
    error:function (xhr, ajaxOptions, thrownError){
        //On error, we alert user
        alert(thrownError);
    }
});
$( "#dialog-form" ).dialog( "open");
});
php代码:

    echo '<textarea class="ckeditor" cols="80" id="fname" name="fname" rows="10" >test</textarea>';
html代码:

  <html>
 <head>
 <script type="text/javascript" src="../include/ckeditor/ckeditor.js"></script>
 <script type="text/javascript" src="../include/ckeditor/sample.js" ></script>
 </head>
 <body>
 <form>
 <fieldset>
 <label for="name">Name</label>
 <div id="resp" ></div>
 </fieldset>
 </form>
 </body>
 </html>

请帮我解决这个问题

您需要手动将textarea转换为CKEditor实例,因为通过分配类名进行替换只在页面加载时完成一次。

有关于如何将文本区域转换为ckeditor实例的示例,基本上只是:

CKEDITOR.replace( 'textarea_id' )

所以你应该加上

CKEDITOR.replace( 'fname' )

到ajax成功回调。

请注意,Javascript是大小写敏感的,所以你应该用大写的CKEDITOR。在replace之后调用ckeditor的函数也过多。