简体   繁体   English

Django-CKEditor小部件无法动态加载

[英]Django-CKEditor Widget not loading dynamically

I am having problem on loading the second and next ckeditor widget on the front-end form in html. 我在加载html前端表单上的第二个和下一个ckeditor小部件时遇到问题。 It works well in admin. 它在管理员中运行良好。 When I click add more form set dynamically, the widget not come out but showing textarea instead, it just works on the first (initalized) form. 当我单击“动态添加更多表单集”时,该窗口小部件不会出现,而是显示textarea,它仅适用于第一个(初始化的)表单。 I already follow the documentation step by step for basic requirements. 我已经按照基本要求逐步遵循文档。 I am using Django with django-ckeditor package. 我正在将Django与django-ckeditor软件包一起使用。 Got no javascript errors on the page. 页面上没有任何JavaScript错误。

Sorry for not showing any codes before. 抱歉,以前没有显示任何代码。 This is part of the javascript which dynamically adding another form set after button clicked: 这是javascript的一部分,它在单击按钮后动态添加了另一个表单集:

<script src="//cdn.ckeditor.com/4.4.5/standard/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor-init.js"></script>

$('#add_more_work').click(function(){
var form_idx = $('#id_form_set-TOTAL_FORMS').val();
$('#form_set_work').append($('#empty_form_work').html().replace(/__prefix__/g, form_idx));
$('#id_form_set-TOTAL_FORMS').val(parseInt(form_idx) + 1);});

The field which use the ckeditor widget not loading after it added dynamically by this button but instead, it shows plain textarea. 使用ckeditor小部件的字段在通过此按钮动态添加后未加载,而是显示纯文本区域。 Did I miss anything? 我有想念吗?

You might want to look at using the formset:added and formset:removed JavaScript signals available from Django 1.9 onwards. 您可能想看看使用从Django 1.9起可用的formset:addedformset:removed JavaScript信号

A simple method (combined from django-content-editor and feincms3 ) might look like this: 一个简单的方法(结合django-content-editorfeincms3 )可能像这样:

(function($) {
  $(document).on('formset:added', function newForm(event, row) {
    row.find('textarea').each(function() {
      CKEDITOR.replace(this.id);
    });
  });
})(django.jQuery);

I'll leave the handling of formset:removed to the reader. 我将把formset:removed的处理留给读者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM