javascript 如何在使用 jquery 的 ajax 操作后清空 TINYMCE 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3874539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:31:18  来源:igfitidea点击:

How to empty TINYMCE content after ajax action with jquery

javascriptjquerytinymce

提问by shin

I add contents in some input fields and textarea with jquery ajax function. Only textare uses TINYMCE.

我使用 jquery ajax 函数在一些输入字段和 textarea 中添加内容。只有 textare 使用 TINYMCE。

However after ajax, the text in TINYMCE does not refresh and remains.

然而在ajax之后,TINYMCE中的文本并没有刷新并保持不变。

How can I empty content in TINYMCE with jquery?

如何使用 jquery 清空 TINYMCE 中的内容?

My current code it following.

我当前的代码如下。

//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;
    });

And the following is a part of HTML

以下是 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...
...

回答by Thariama

You do not need jQuery to empty tinymce. Get the tinymce instance by id and set the content to ''(equals empty) using

您不需要 jQuery 来清空 tinymce。通过 id 获取 tinymce 实例并将内容设置为''(等于空)使用

//the id of the first editorinstance of the page is to be found in tinymce.editors[0].id

//页面第一个editorinstance的id在tinymce.editors[0].id中找到

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

回答by JohnnyQ

This worked for me:

这对我有用:

tinyMCE.activeEditor.setContent('');

Especially if it's the only editor existing in your page.

特别是如果它是您页面中唯一存在的编辑器。

回答by Hemant kumar sharma

Try with below code

尝试使用以下代码

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