简体   繁体   中英

Get the value from SCEditor

I'm trying to get the value from my SCEditor textarea, using this code:

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]');
});

I then want to send the value via AJAX:

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

However, I'm not able to get this to work. I haven't found find any tips in the documentation: http://www.sceditor.com/api/sceditor/val/

My variable fbeschreibung is just empty. Is there something I've done wrong?

This worked for me:

$(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();
});

I am not familiar with this particular editor, but the options (comments) I provided you may work.

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]');
});

I was experiencing some issues getting the JQuery code for this to work, and found that this can actually be done without using JQuery at all using:

sceditor.instance(textarea).val()

Where textarea is the textarea that your editor was created from:

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
}

If you have multiple SCEditors on the page, you can just use pass a different textarea to the sceditor.instance function

(More info on the Usage section on SCEditor's GitHub )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM