tinyMCE.get(“content”)未定义

时间:2011-11-24 14:04:20

标签: javascript html tinymce

我在Firebug上收到此消息:tinyMCE.get("message_data_" + msg_id) is undefined 我点击按钮提交表格。

msg_id 定义,我查了一下。 message_data_也已定义。 函数tinyMCE.get不是出于某种原因。

3 个答案:

答案 0 :(得分:4)

如果您使用的是单个编辑器实例,则可以使用tinymce.editors[0]代替tinyMCE.get("message_data_" + msg_id)

答案 1 :(得分:1)

如果您无法控制TinyMCE的init方法,则可以按照此解决方案进行操作。基本上,如果没有初始化TinyMCE,它会增加一个后备。

jQuery(document).ready(function($) {

    function myCustomSetContent( id, content ) {
        // Check if TinyMCE is defined or not.
        if( typeof tinymce != "undefined" ) {
            var editor = tinymce.get( id );
            // Check if TinyMCE is initialized properly or not.
            if( editor && editor instanceof tinymce.Editor ) {
                editor.setContent( text );
                editor.save( { no_events: true } );
            } else {
                // Fallback
                // If TinyMCE is not initialized then directly set the value in textarea.
                //TinyMCE will take up this value when it gets initialized.
                jQuery( '#'+id ).val( text );
            }
            return true;
        }
        return false;
    }

    function myCustomGetContent( id ) {
        // Check if TinyMCE is defined or not.
        if( typeof tinymce != "undefined" ) {
            var editor = tinymce.get( id );
            // Check if TinyMCE is initialized properly or not.
            if( editor && editor instanceof tinymce.Editor ) {
                return editor.getContent();
            } else {
                // Fallback
                // If TinyMCE is not initialized then directly set the value in textarea.
                // TinyMCE will take up this value when it gets initialized.
                return jQuery( '#'+id ).val();
            }
        }
        return '';
    }

    $(".class-to-update-content").on("click", function(e) {
        myCustomSetContent( "tinymce-editor-id", "New Content in Editor" );
    });

    $(".class-to-get-content").on("click", function(e) {
        $("div.class-to-display-content").html( myCustomGetContent( "tinymce-editor-id" ) );
    });
});

参考:http://blog.incognitech.in/tinymce-undefined-issue/

答案 2 :(得分:0)

如果您在表单中使用多个编辑器,则可以创建一个函数并获取其值 即。

// Declare a function to get editor value
function tinyMca_text(field_id) {

 if ( jQuery("#"+field_id+":hidden").length > 0)
 {
 return tinyMCE.get(field_id).getContent();
 }
 else
 {
  return jQuery('#'+field_id).val();
 }

}

// show that value

console.log(tinyMca_text('field'));