我想在引导模式中显示 ckEditor,但 ckEditor 没有打开,如下所示。通过互联网搜索后,我以不同的方式得到了一些解决方案,如 this link但我将执行一个 ajax 请求,因此我必须使用 $('.cia_modal').modal('toggle')
打开模式。上述解决方案链接对我不起作用
模态按钮:
<button class="btn btn-info btn-xs cia_modal_btn" data-id="<?php echo $post->post_id; ?>" data-table="post" data-attr="post_id">
Create New Post
</button>
模态:
<!--Modal-->
<div class="modal fade cia_modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-primary">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title">Create New Post</h4>
</div>
<div class="modal-body">
<form action="/action_page.php">
<div class="form-group">
<textarea class="ck_editor" name="post" id="" cols="30" rows="10"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!--End Modal-->
Javascript:
<script type="text/javascript">
$(document).on("click", ".modal_btn", function (e) {
e.preventDefault();
e.stopPropagation();
let cia_modal = $('.cia_modal');
//Set modal show/hide
cia_modal.modal('toggle');
let id = $(this).attr('data-id');
let table = $(this).attr('data-table');
let attr = $(this).attr('data-attr');
//Set Form Data
let formData = new FormData();
formData.append('id',id);
formData.append('table',table);
formData.append('attr',attr);
$.ajax({
type: 'post',
url: '<?php echo base_url()?>post/create_post',
data: formData,
contentType: false,
processData: false,
success: function (data) {
//
}
});
});
</script>
请帮帮我。谢谢
最佳答案
我假设您使用的是 CKEditor 4.x。
您需要使用 CKEDITOR.replace( 'ck_editor' )
创建编辑器,您只能在文档中存在的元素上执行此操作。隐不隐没关系。如果你的模式在 DOM 中,但隐藏了,那么只需初始化编辑器。否则你需要等到元素被追加然后调用 CKEDITOR.replace
。
取决于 Bootstrap 如何实现 modal.show
。如果它只是更改 CSS 以使其可见,那么很可能您可以在脚本开始时初始化编辑器,如果它向文档添加元素,则您需要使用一些引导事件在正确的时间创建编辑器。参见 shown.bs.modal
:https://getbootstrap.com/docs/4.0/components/modal/#events
关于javascript - CKEditor 不适用于 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374102/