javascript - 将 CKEditor 分配给动态创建的文本框元素
问题描述
我有一个表结构,其中CKEditor附加到表 td。像这样的东西:
HTML:
<table class="table table-striped table-condensed" id="questionDetails">
<thead>
<tr>
<th>Question Name</th>
<th>Options</th>
<th>Answers</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td class="col-xs-3">
<input type="text" class="txtQuestionName form-control" name="txtQuestionName" value="" />
</td>
<td class="col-xs-3">
<input type="text" class="txtOptions form-control" name="txtOptions" value="" />
<span class="addOptions">(+)</span>
</td>
<td class="col-xs-3">
<input type="text" class="txtAnswers form-control" value="" />
<span class="addAnswers">(+)</span>
</td>
<td class="col-xs-12">
<input type="text" class="txtExplanation form-control editor1" name="editor1" />
</td>
<td>
<a href="javascript:void(0);" id="btnAdd" class="btn btn-primary">Add</a>
</td>
</tr>
</tbody>
</table>
jQuery :
<script src="~/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
//Initialize CKEditor by giving id of text area
CKEDITOR.replace('editor1');
//Get each instance of CKEditor
for (instance in CKEDITOR.instances) {
//update element
CKEDITOR.instances[instance].updateElement();
}
//Add row to the table
$("#btnAdd").click(function () {
$("#questionDetails").append('<tr><td><input type="text" class="txtQuestionName form-control" class="form-control" /></td> <td><input type="text" class="txtOptions form-control" /><span class="addOptions">(+)</span></td> <td><input type="text" class="txtAnswers form-control" /> <span class="addAnswers">(+)</span></td><td><input type="text" class="txtExplanation form-control editor1" name="editor1" /></td> <td><a href="javascript:void(0);" class="btn btn-danger remCF">Remove</a></td> </tr>');
});
</script>
上面的代码在默认加载页面时有效。但是,当我尝试使用动态向表中添加更多行时jQuery
,不会创建CKEditor或将其分配给该输入元素。除编辑器外,其他输入元素均已创建。有什么方法可以将CKEditor分配给表中动态创建的行并从中获取所有值?
解决方案
这是一个工作示例。您必须确保为所有编辑者提供唯一 ID
<div>
<textarea id="editor1"></textarea>
</div>
<div>
<button type="button" id="btnAdd">Add CKEditor</button>
</div>
<div id="questionDetails">
</div>
<script type="text/javascript" src="~/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
//the inital editor
CKEDITOR.replace('editor1');
//use an number to make the id's of the new editors unique
var index = 2;
//Add row to the table
$("#btnAdd").click(function () {
//create the new id
var newEditor = 'editor' + index;
//append some html
$("#questionDetails").append('<textarea id="' + newEditor + '"></textarea>');
//init the new editor
CKEDITOR.replace(newEditor);
//increment
index++;
});
</script>
如果要读取内容,只需循环实例并获取数据getData()
$("#btnRead").click(function () {
for (var i in CKEDITOR.instances) {
alert(CKEDITOR.instances[i].getData());
}
});
推荐阅读
- ruby-on-rails - Phusion 乘客进程显示出非常不同的内存使用情况
- javascript - 如何使用 URL 中的查询字符串在 PhpStorm 中运行 Node.js 应用程序?
- unity3d - 如何使用图像组件在 Unity 中创建遮罩?
- r - 1 个定量变量和 2 个定性变量的 Levene 检验
- python - 字符串中唯一字符的计数和返回值
- javascript - 想要使用 Sequelize 从对象数组中删除任务
- java - @OneToOne 关系 Spring Data JPA/Hibernate 实体关系
- php - 在删除记录之前检查 Laravel 8 中是否存在 hasMany 关系
- python - Tkinter 引用方法和类之间的变量
- javascript - 与我的 quasar 应用程序连接的后端套接字 io 出现问题。Socket io 出现代码 403(禁止)错误