使用 JQuery 设置 CKEditor 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5519674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:17:29  来源:igfitidea点击:

Using JQuery to set CKEditor Value

jqueryajaxckeditor

提问by balexander

I have a CKEditor textarea:

我有一个 CKEditor 文本区域:

 <textarea cols="80" id="taBody" name="taBody" class="ckeditor" rows="10" runat="server"></textarea>

I have jQuery trying to set the value from the database:

我有 jQuery 试图从数据库中设置值:

$('#ContentPlaceHolder_taBody').val(substr[5]);

Don't worry about the substring I already tested that it is returning a string. For testing purposes I replaced the substring with 'test' and was receiving the same issue.

不要担心我已经测试过它正在返回一个字符串的子字符串。出于测试目的,我用 'test' 替换了子字符串并收到了同样的问题。

I know that the jquery surrounding this line doesn't affect it because the other textfields I'm trying to populate work. Just when it comes to the ckeditor.

我知道围绕这一行的 jquery 不会影响它,因为我试图填充的其他文本字段工作。就在谈到 ckeditor 时。

Here is the script in whole:

这是整个脚本:

function (obj) {
      $.ajax({
         type: "POST",
          url: "ContentSections.aspx/GetContentDetails",
          data: '{"nodeID": "' + obj.attr('id') + '"}',
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {
             var str = msg.d;
             var substr = str.split('|||');

             $('#ContentPlaceHolder_hfContentSectionID').val(substr[0]);
             $('.txtAlias').val(substr[1]);
             $('.txtBrowserTitle').val(substr[2]);
             $('.txtMetaDescription').val(substr[3]);
             $('.txtMetaKeywords').val(substr[4]);
             $('#ContentPlaceHolder_taBody').val(substr[5]);
          }
     });
}

The issue was that nothing was being populated and no javascript errors were being shown.

问题是没有填充任何内容,也没有显示 javascript 错误。

I tried to read around but couldn't find anything that helped me. Does anyone have any ideas?

我试着四处阅读,但找不到任何对我有帮助的东西。有没有人有任何想法?

回答by simshaun

You need to use CKEditor's API instead.

您需要改用 CKEditor 的 API。

Specifically, http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setData

具体来说,http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setData

回答by Ravi Mane

After reading this link http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setData, following code work for me.

阅读此链接http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setData 后,以下代码对我有用

CKEDITOR.instances.editor1.setData( '<p>This is the editor data.</p>' );