Ckeditor不加载通过ajax调用生成的元素

ckeditor not loading on element generated via ajax call?

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

我使用自定义表单和生成表单元素与ajax调用,但textarea没有加载ckeditor。下面是我的代码:

ajax代码:

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

ajax响应是:

   '<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>

请帮我解决这个问题

插入以下行:

ckeditor.replace('#fname'); // ADD THIS
$('#fname').ckeditor(); // ADD THIS

你的代码应该是这样的:

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

我只有这一行工作:

ckeditor.replace('#fname');

和下面一行需要删除:

$('#fname').ckeditor(); // this does NOT work

还要注意,ckeditor需要大写,所以:

CKEDITOR.replace('#fname');

只添加CKEDITOR.replace('fname');#是不必要的。此外,您不必添加:

$('#fname').ckeditor();

请确保全大写,例如CKEDITOR而不是CKEDITOR

不添加ckeditor.replace('#fname');你必须添加$('#fname').ckeditor();在我的项目中是works

编辑这一行:

CKEDITOR.replace( 'fname' );

你的代码应该是这样的:

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