2010-10-06 320 views
7

我在jQuery ajax函數的某些輸入字段和textarea中添加內容。只有textare使用TINYMCE。如何使用jquery執行ajax操作後清空TINYMCE內容

但是,在ajax之後,TINYMCE中的文本不會刷新並保留。

如何用jquery清空TINYMCE中的內容?

我目前的代碼如下。

//on submit event 
    $("#specformentry").submit(function(event){ 
     event.preventDefault(); 
     if(checkForm()){ 
      // var href = $(this).attr("href"); 
      submitinput.attr({ disabled:true, value:"Sending..." }); 
      //$("#send").blur(); 
      //send the post to shoutbox.php 
      $.ajax({ 
       type: "POST", 
       url: "../../Ajaxinsertspec", 
       data: $('#specformentry').serialize(), 
       complete: function(data){ 
        update_entry(); 
        specdesc.val(''); 
        datecreated.val(''); 
        detailstext.val(''); 
       // this code is supposed to empty the INYMCE content, but it does not 

        //reactivate the send button 
        submitinput.attr({ disabled:false, value:"Enter Spec" }); 
       } 
      }); 
     } 
     else alert("Please fill all fields!"); 
     //we prevent the refresh of the page after submitting the form 
     return false; 
    }); 

而下面是HTML

<div id="enterlabel"><label for="spec_details">Click me to enter Spec Details</label></div> 
<div style="display: block;" id="textarea"> 
<textarea style="display: none;" name="spec_details" cols="90" rows="12" id="detailstext"></textarea> 
<span class="mceEditor defaultSkin" id="detailstext_parent"> 
    <table style="width: 100px; height: 100px;" class="mceLayout" id="detailstext_tbl" cellpadding="0" cellspacing="0"> 
     <tbody><tr class="mceFirst"> 
      <td class="mceToolbar mceLeft mceFirst mceLast"><a href="#" accesskey="q" title="Jump to tool buttons - Alt+Q, Jump to editor - Alt-Z, Jump to... 
... 

回答

14

的一部分,你並不需要jQuery來清空TinyMCE的。由ID獲取TinyMCE的實例,並使用設定的內容,以''(等於空)

//頁面的第一editorinstance 的id爲 tinymce.editors被發現[0] .ID

var tinymce_editor_id = 'my_tinymce_id'; 
tinymce.get(tinymce_editor_id).setContent(''); 
+0

感謝解決我的問題.. 2012-11-16 08:17:16

+0

非常有幫助 – Thariama 2012-11-16 08:58:13

2

嘗試用下面的代碼

if (typeof(tinyMCE) != 'undefined') { 
    $('#edit-comment').val(''); // Removes all paragraphs in the active editor 
} 
8

這爲我工作:

tinyMCE.activeEditor.setContent(''); 

尤其是如果它是您網頁中唯一存在的編輯器。

+0

剛夠我的情況,沒有更多!:) – lean 2016-12-30 10:29:43