从 SCEditor 获取值

Get the value from SCEditor

我正在尝试使用以下代码从我的 SCEditor 文本区域获取值:

var fbeschreibung = '';
$(function() {
    // Replace all textarea's
    // with SCEditor
    $("textarea").sceditor({
        plugins: "bbcode",
        style: "css/style.less",
        width: "100%",
        toolbar:"bold,italic,underline,strike,subscript,superscript|left,center,right,justify|size,color,removeformat|bulletlist,orderedlist,horizontalrule,emoticon",
        locale:"de" ,
        resizeEnabled:false
    });
    fbeschreibung = $('textarea').sceditor('instance').val();
    $('textarea').sceditor('instance').val('Hello [b]World![/b]');
});

然后我想通过 AJAX 发送值:

$.post('saveprofile.php', 
   {fbeschreibung : fbeschreibung},
   function (response) {
      alert(response);
   }
);

但是,我无法让它工作。我没有在文档中找到任何提示:http://www.sceditor.com/api/sceditor/val/

我的变量 fbeschreibung 是空的。是不是我做错了什么?

我不熟悉这个特定的编辑器,但我提供给您的选项(评论)可能有用。

var fbeschreibung = '';
$(function() {
    // Replace all textarea's
    // with SCEditor
    var editor = $("textarea").sceditor({
        plugins: "bbcode",
        style: "css/style.less",
        width: "100%",
        toolbar:"bold,italic,underline,strike,subscript,superscript|left,center,right,justify|size,color,removeformat|bulletlist,orderedlist,horizontalrule,emoticon",
        locale:"de" ,
        resizeEnabled:false
    });
    /* Try the following 3 options */

    fbeschreibung = editor.val();
    fbeschreibung = editor.getSourceEditorValue();
    fbeschreibung = editor.getWysiwygEditorValue();

    editor.val('Hello [b]World![/b]');
});

这对我有用:

$(function() {
    // Replace all textarea's
    // with SCEditor
    var fbeschreibung = $("textarea").sceditor({
        plugins: "bbcode",
        style: "css/style.less",
        width: "100%",
        toolbar:"bold,italic,underline,strike,subscript,superscript|left,center,right,justify|size,color,removeformat|bulletlist,orderedlist,horizontalrule,emoticon",
        locale:"de" ,
        resizeEnabled:false
    }).sceditor('instance');
    var value = fbeschreibung.getBody().html();
});

我在获取 JQuery 代码使其工作时遇到了一些问题,并且发现这实际上可以在不使用 JQuery 的情况下完成,使用:

sceditor.instance(textarea).val()

其中 textarea 是创建编辑器的文本区域:

let textarea = document.getElementById('my-textarea');

// the sceditor variable is created by importing the sceditor JavaScript code
// (sceditor.min.js and formats/bbcode.js)
sceditor.create(textarea, {
    format: 'bbcode',
    style: '/minified/themes/content/default.min.css'
});

function logEditorValue(){
   let textarea = document.getElementById('my-textarea');
   let value = sceditor.instance(textarea).val();

   console.log(value); // prints the value of the sceditor
}

如果页面上有多个 SCEditor,您只需将不同的 textarea 传递给 sceditor.instance 函数

(有关 Usage section on SCEditor's GitHub 的更多信息)