How can i get content of CKEditor using JQuery?

前端 未结 15 1798
甜味超标
甜味超标 2020-12-04 15:21

I\'m using CKEditor. I am saving the form values with ajax using page methods.

But the content of CKEditor value cannot be saving into the table.

I dont post

相关标签:
15条回答
  • 2020-12-04 16:01

    Using Pure Vanilla Javascript / Jquery or in any javascript library :

    If you have Ckeditor loaded in below text-area:

     <textarea name="editor1" id="editor1"></textarea>
    

    Then you can get content inside textarea as below:

    var txtNotes = document.getElementsByClassName('ck-content')[0].innerHTML;
    
    0 讨论(0)
  • 2020-12-04 16:02

    Thanks to John Magnolia. This is my postForm function that I am using in my Symfony projects and it is fine now to work with CK Editor.

    function postForm($form, callback)
    {
      // Get all form values
      var values = {};
      var fields = {};
    
      for(var instanceName in CKEDITOR.instances){
          CKEDITOR.instances[instanceName].updateElement();
      }
    
      $.each($form.serializeArray(), function(i, field) {
          values[field.name] = field.value;
      });
    
      // Throw the form values to the server!
      $.ajax({
          type        : $form.attr('method'),
          url         : $form.attr('action'),
          data        : values,
          success     : function(data) {
              callback( data );
          }
      });
    
    0 讨论(0)
  • 2020-12-04 16:03


    i add ckEditor by adding DLL in toolBox.
    html code:

    <CKEditor:CKEditorControl ID="editor1" runat="server" 
                BasePath="ckeditor" ContentsCss="ckeditor/contents.css" 
                Height="250px" 
                TemplatesFiles="ckeditor/themes/default/theme.js" FilebrowserBrowseUrl="ckeditor/plugins/FileManager/index.html" 
                FilebrowserFlashBrowseUrl="ckeditor/plugins/FileManager/index.html" FilebrowserFlashUploadUrl="ckeditor/plugins/FileManager/index.html" 
                FilebrowserImageBrowseLinkUrl="ckeditor/plugins/FileManager/index.html" FilebrowserImageBrowseUrl="ckeditor/plugins/FileManager/index.html" 
                FilebrowserImageUploadUrl="ckeditor/plugins/FileManager/index.html" 
                FilebrowserUploadUrl="ckeditor/plugins/FileManager/index.html" BackColor="#FF0066" 
                        DialogButtonsOrder="Rtl" 
                        FontNames="B Yekan;B Yekan,tahoma;Arial/Arial, Helvetica, sans-serif; Comic Sans MS/Comic Sans MS, cursive; Courier New/Courier New, Courier, monospace; Georgia/Georgia, serif; Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif; Tahoma/Tahoma, Geneva, sans-serif; Times New Roman/Times New Roman, Times, serif; Trebuchet MS/Trebuchet MS, Helvetica, sans-serif; Verdana/Verdana, Geneva, sans-serif" 
                        ResizeDir="Vertical" ResizeMinHeight="350" UIColor="#CACACA">dhd fdh</CKEditor:CKEditorControl>
    

    for get data of it.
    jquery:

    var editor  = $('textarea iframe html body').html();
        alert(editor); 
    
    0 讨论(0)
  • 2020-12-04 16:04

    I was having issues with the getData() not working every time especially when dealing with live ajax.

    Was able to get around it by running:

    for(var instanceName in CKEDITOR.instances){
        CKEDITOR.instances[instanceName].updateElement();
    }
    

    Then use jquery to get the value from the textarea.

    0 讨论(0)
  • 2020-12-04 16:04

    I think it will be better, just serialize your form by jquery and cheers...

    <form id="ajxForm">
      <!-- input elments here -->
      <textarea id="ck-editor" name="ck-editor" required></textarea>
      <input name="text" id="text" type="text" required> 
    <form>
    

    and In javascript section

    CKEDITOR.replace('ck-editor', {
      extraPlugins: 'sourcedialog',
      removePlugins: 'sourcearea'
    });
    
    $("form#ajxForm").submit(function(e) {
      e.preventDefault();
      var data = $(this).serialize();
      if (data != '') {
        $.ajax({
          url: 'post.php',
          cache: false,
          type: 'POST',
          data: data,
          success: function(e) {
            setTimeout(function() {
              alert(e);
            }, 6500);
          }
        });
      }
      return;
    });
    
    0 讨论(0)
  • 2020-12-04 16:06

    version 4.8.0

    $('textarea').data('ckeditorInstance').getData();
    
    0 讨论(0)
提交回复
热议问题