TinyMCE textarea 和 post 表单使用 ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14393512/
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-09-06 10:55:26  来源:igfitidea点击:

TinyMCE textarea and post form using ajax

ajaxtinymcesave

提问by Sanjay Panchal

I m using tinyMCE for textareas and POSTing form through AJAX.

我正在通过 AJAX 将 tinyMCE 用于 textareas 和 POSTing 表单。

But when I m trying to save textarea value, it is taking old values on first click, but it takes updated values on second click.

但是当我尝试保存 textarea 值时,它在第一次单击时采用旧值,但在第二次单击时采用更新值。

I have tried using tinyMCE.triggerSave()but it didn't work.

我试过使用,tinyMCE.triggerSave()但没有用。

I have also tried tinyMCE.get('myid').getContent(), still it takes old values.

我也试过tinyMCE.get('myid').getContent(),它仍然需要旧值。

My code is as follows.

我的代码如下。

    $(".submit").live("click", function () {
            tinyMCE.triggerSave();
            var f = $(this).parents("form");
            var action = f.attr("action");
            var serializedForm = f.serialize();
            //tinyMCE.triggerSave(); also tried putting here
            $.ajax({
                 type: 'POST',
                 url: action,
                 data: serializedForm,
                 async: false,
                 success: function (data, textStatus, request) {
                     $(".divform").html(data);
                 },
                 error: function (req, status, error) {
                     alert&("Error occurred!");
                 }
            });
    return false;
    });

Please help, any help would be appreciated

请帮忙,任何帮助将不胜感激

采纳答案by Thariama

Use this instead of tinymce.triggerSave();

用这个代替 tinymce.triggerSave();

$('#' + 'your_editor_id').html( tinymce.get('your_editor_id').getContent() );

回答by Dan Malcolm

You can configure TinyMCE as follows to keep the values of hidden textareas in sync as changes are made via TinyMCE editors:

您可以按如下方式配置 TinyMCE,以使隐藏文本区域的值在通过 TinyMCE 编辑器进行更改时保持同步:

tinymce.init({
    selector: "textarea",
    setup: function (editor) {
        editor.on('change', function () {
            tinymce.triggerSave();
        });
    }
});

With this in place, you can access up-to-date values directly from the textarea elements at any time.

有了这个,您可以随时直接从 textarea 元素访问最新值。

This has been tested on TinyMCE 4.0

这已经在 TinyMCE 4.0 上测试过了

Demo running at: http://jsfiddle.net/9euk9/

演示运行在:http: //jsfiddle.net/9euk9/

回答by HappyDog

An alternative implementation to the one posted by Dan Malcolm, for TinyMCE 3.x, would be as follows:

对于 TinyMCE 3.x,Dan Malcolm 发布的替代实现如下:

tinymce.init({
    selector: "textarea",
    setup: function (editor) {
        editor.onChange.add(function() {
            editor.save();
        });
    }
});

As well as working on 3.x, this version uses editor.saveinstead of tinymce.triggerSave, which means it only updates the current editor rather than all editors in the page.

除了在 3.x 上工作之外,此版本还使用了editor.save代替tinymce.triggerSave,这意味着它只更新当前编辑器而不是页面中的所有编辑器。

回答by user2828818

user this script before posting data by using Ajax. This is javascript code before use please load tiny mce js file and user it.

在使用 Ajax 发布数据之前使用此脚本。这是使用前的 javascript 代码,请加载微小的 mce js 文件并使用它。

tinymce.triggerSave();

tinymce.triggerSave();

$.ajax({ 
    type: 'post', 
    url: 'autoSaveReport.php', 
    data: $('form').serialize(), 
    success: function (result) { 
        var redirectURL = window.location.pathname; 
        var redirectURL1 = redirectURL+"?incid="+result; 
        window.location = window.location+"?incid="+result; 
    } 
});

回答by speedygonzales77

@Dan Malcom,

@丹马尔科姆,

I noticed that when you type something in one of the boxes, then press the "show text values" button, then if you click the "Undo" arrow, it just keeps the already displayed text, not the new text caused by the "Undo". I discovered it while trying to use your example to check that the texteditor had something in it.

我注意到当您在其中一个框中键入内容时,然后按“显示文本值”按钮,然后如果您单击“撤消”箭头,它只会保留已显示的文本,而不是“撤消”导致的新文本”。我在尝试使用您的示例检查文本编辑器中是否包含某些内容时发现了它。

In my example if I type something in it, then do "Undo" it removes the text in it, but the form still submits.

在我的示例中,如果我在其中键入内容,然后执行“撤消”操作,它会删除其中的文本,但表单仍会提交。

See example here:

请参阅此处的示例:

enter code here

https://codepen.io/speedygonzales77/pen/bzMrqB

https://codepen.io/speedygonzales77/pen/bzMrqB

enter code here