Using jQuery to grab the content from CKEditor'

2019-01-06 11:25发布

I have a custom-written CMS that uses CKEditor *(FCKEditor v3) for editing content. I'm also using the jQuery Validation plugin to check all fields for error prior to AJAX-based submission. I'm using the serialize() function to passing the data to the PHP backend.

Problem is, serialize manages to grab all fields correctly, except for the actual content typed in CKEditor. Like every other WYSIWYG editor, this one too overlays an iframe over an existing textbox. And serialize ignores the iframe and looks only into the textbox for content, which, of course, it doesn't find, thus returning a blank content body.

My approach to this is to create a hook onto the onchange event of CKEditor and concurrently update the textbox (CKEDITOR.instances.[textboxname].getData() returns the content) or some other hidden field with any changes made in the editor.

However, since CKEditor is still in it's beta stage and severely lacks documentation, I can't find a suitable API call that'll enable me to do so.

Does anyone have any idea on how to go about this?

12条回答
可以哭但决不认输i
2楼-- · 2019-01-06 12:14

I had success with this:

console.log(CKEDITOR.instances.editor1.getData());
查看更多
做自己的国王
3楼-- · 2019-01-06 12:15

Wouldn't it be better to do just this:

CKEDITOR.instances.editor1.on('contentDom', function() {
          CKEDITOR.instances.editor1.document.on('keyup', function(event) {/*your instructions*/});
        });

ref: http://cksource.com/forums/viewtopic.php?f=11&t=18286

查看更多
\"骚年 ilove
4楼-- · 2019-01-06 12:17

I took a slightly different approach I figured it would be better to use ckeditor's update function and since keyup was being used the timeout wasn't needed

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
}

function CK_jQ()
{
   CKEDITOR.instances.editor1.updateElement(); 
}
查看更多
对你真心纯属浪费
5楼-- · 2019-01-06 12:20

Another generic solutions to this would be to run the following whenever you try to submit the form

for ( instance in CKEDITOR.instances )
            CKEDITOR.instances[instance].updateElement();

This will force all CKEDITOR instances in the form to update their respective fields

查看更多
SAY GOODBYE
6楼-- · 2019-01-06 12:20

I've been trying all night to get this to work but it isn't working yet. Could you please explain where you placed this script?

I am generating my page from an xquery so I can't put this script in the page because it contains "{" which breaks the xquery processing. Putting the script in cdata breaks the script. So I put the on instanceReady listener in the same script that is creating the editor and called an external script to add the rest. eg:

<script type="text/javascript">
  var editor = CKEDITOR.replace( 'editor1' );
  editor.on("instanceReady", updateInstance() )
</script>

then updateInstance contains:

function updateInstance()
{
CKEDITOR.instances["editor1"].document.on('keydown', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

CKEDITOR.instances["editor1"].document.on('paste', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

}
查看更多
神经病院院长
7楼-- · 2019-01-06 12:21

The contentDom event worked for me and not the instanceReady... I would really like to know what the events ae, but I assume they are proprietary...

var editor = CKEDITOR.replace('editor');

CKEDITOR.instances.editor.on("instanceReady", function(){
    this.on('contentDom', function() {
        this.document.on('keydown', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    this.on('contentDom', function() {
        this.document.on('paste', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    edits_clix();
    var td = setTimeout("ebuttons()", 1);
})
查看更多
登录 后发表回答