ckeditor没有加载弹出对话框中通过ajax生成的元素?

时间:2014-05-20 12:48:51

标签: javascript php jquery html ajax

我正在使用自定义表单并使用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>

请帮我解决问题。

1 个答案:

答案 0 :(得分:1)

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

There are samples on how to convert a textarea to a ckeditor instance,基本上只是:

CKEDITOR.replace( 'textarea_id' )

所以在你的情况下你应该添加

CKEDITOR.replace( 'fname' )

到ajax成功回调。

请注意 Javascript区分大小写,因此您应该以大写形式编写CKEDITORckeditor之后的函数调用replace也太多了。请参阅示例以获取官方指南。