2010-07-07 66 views
0

我想使用AJAX做一個管理頁面,因此當客戶端更新了CKEDITOR中的信息時,它不必將他帶到新頁面。使用.val()函數從輸入字段獲取數據非常容易,但由於textareas並未實時更新,因此我無法使用該功能。繼承人據我:CKEDITOR ajax帖子

//這個替換所有的textarea標籤到CKEDITORS

<script type="text/javascript"> 
    CKEDITOR.replaceAll(); 
</script> 

//這個試圖抓住從輸入的所有數據和文字區域

$(function() { 
     $("#submit").click(function() { 
      var newsTitle = $("#newsTitle").val(); 
      var editNews = CKEDITOR.instances.editNews.getData(); 
      var contactTitle = $("#contactTitle").val(); 
      var editContact = CKEDITOR.instances.editContact.getData(); 
      var linksTitle = $("#linksTitle").val(); 
      var editLinks = CKEDITOR.instances.editLinks.getData(); 

       $.ajax({ 
        type: "POST", 
        url: "update.php", 
        data: 'newsTitle='+newsTitle+'&editNews='+editNews+'&contactTitle='+contactTitle+'&editContact='+editContact+'&linksTitle='+linksTitle+'&editLinks='+editLinks, 
        cache: false, 
        success: function(){ 
         updated(); 
        } 

       });  

      return false; 
     }); 
    }); 

中的getData()功能似乎是可行的,因爲我用警報測試它,它從編輯器中獲取數據,但是一旦我嘗試更新,它將無法工作...

有什麼想法?

回答

1

踏歌來看看CKEditor的功能/適配器的jQuery

http://docs.cksource.com/CKEditor_3.x/Developers_Guide/jQuery_Adapter

Because setting and retrieving the editor data is a common operation, the jQuery Adapter also provides the dedicated val() method:

// Get the editor data. 
var data = $('textarea.editor').val(); 
// Set the editor data. 
$('textarea.editor').val('my new content'); 
+0

我剛剛嘗試過適配器,並且val()方法確實爲警報提供了正確的代碼,但仍拒絕發佈數據。 – Ohnegott 2010-07-07 12:30:32

+0

它可能仍然是某種函數/對象,嘗試執行'$('/ ...').val()。toString()'返回'alert'顯示的上下文 – RobertPitt 2010-07-07 12:41:22

3

此代碼替換textarea的:

<script type="text/javascript"> 

CKEDITOR.replace('TEXTAREA_ID', { 
    extraPlugins : 'autogrow', 
    removePlugins : 'resize', 
    entities : false 
}); 

</script> 

在JS文件,這是代碼和我正在使用JQuery的Validator插件:

$(document).ready(function(){ 

jQuery.validator.messages.required = ""; 

$("#FormID").validate({ 
    submitHandler: function(){ 

     var ContentFromEditor = CKEDITOR.instances.TEXTAREA_ID.getData(); 

     var dataString = $("#FormID").serialize(); 

      dataString += '&ContentFromEditor='+ContentFromEditor;   


     $.ajax({ 
     type: "POST", 
     url: "Yourfile.php", 
     data: dataString, 
     cache: false, 
     success: function(html){ 
      YOU WORK WITH THE RETURN HERE 
     }, 
     error: function(xhr, ajaxOptions, thrownError){ 
      alert(xhr.responseText); 
     } 
    }); 

    return false; 

    } 
}); 

}); 

這是大部分時間造成錯誤的行:

CKEDITOR.instances.TEXTAREA_ID.getData(); 

的情況後總是textarea的ID。

我有我自己的config.js,你可以從ckeditor網站或從例子中獲得。

1

有了這段代碼,我的問題就解決了。 我更新了運行ckeditor的字段,以便在序列化中看到。

$('#form').find('.class').each(function(index) { 
    $(this).val(CKEDITOR.instances[$(this).attr('id')].getData()); 
}); 
+0

謝謝,這對我有效。我使用了.find('textarea')。each(function(index){(this).val(CKEDITOR.instances [$(this).attr('id')] .getData()); }) ; – 2016-12-12 16:01:49