简体   繁体   English

用 AJAX 和 Jquery 保存 tinymce 编辑器

[英]Saving tinymce editor with AJAX and Jquery

I've been reading similar questions to this issue and have been able to get pretty far, but apparently my situation is slightly different so I'm still trying to figure this out.我一直在阅读与此问题类似的问题,并且能够走得很远,但显然我的情况略有不同,所以我仍在努力解决这个问题。

I have a form that has textareas that are styled with the Tinymce html editor.我有一个表单,其中包含使用 Tinymce html 编辑器设置样式的文本区域。 I would like the textarea to autosave with AJAX.我希望文本区域自动保存为 AJAX。

I have been working with code that saves the textarea based on a time interval:我一直在使用基于时间间隔保存文本区域的代码:

$(document).ready(function() {

$(function() {
// Here we have the auto_save() function run every 30 secs
    // We also pass the argument 'editor_id' which is the ID for the textarea tag
    setInterval("auto_save('editor_id')",30000);
});

}); });

// Here is the auto_save() function that will be called every 30 secs
function auto_save(editor_id) {

// First we check if any changes have been made to the editor window
    if(tinyMCE.getInstanceById(editor_id).isDirty()) {
    // If so, then we start the auto-save process
        // First we get the content in the editor window and make it URL friendly
        var content = tinyMCE.get(editor_id);
        var notDirty = tinyMCE.get(editor_id);
        content = escape(content.getContent());
        content = content.replace("+", "%2B");
        content = content.replace("/", "%2F");
        // We then start our jQuery AJAX function
        $.ajax({
        url: "PAFormAJAX.asp", // the path/name that will process our request
            type: "POST", 
            data: "itemValue=" + content, 
            success: function(msg) {
                alert(msg);
                // Here we reset the editor's changed (dirty) status
                // This prevents the editor from performing another auto-save
                // until more changes are made
                notDirty.isNotDirty = true;
            }
        });
        // If nothing has changed, don't do anything
    } else {
        return false;
    }
}

This is working, but my problem is, the form elements are created dynamically so I don't always have static editor_id's that I can use.这是有效的,但我的问题是,表单元素是动态创建的,所以我并不总是有 static editor_id 可以使用。 How can I update it to accept dynamic ID's?我如何更新它以接受动态 ID?

For example, here's one of the textareas with it's id being dynamically set with ASP:例如,这里是其中一个文本区域,它的 id 是用 ASP 动态设置的:

<textarea id="Com<%=QuesID%>" row= "1" cols= "120" name="Com<%=QuesID%>" QuesID="<%=QuesID%>" wrap tabindex="21" rows="10" class="formTxt"><%=TempTxt%></textarea>

Also, I'm trying to figure out a way to not only call the save function on a time interval, but when the user clicks out of the textarea and it looses focus.此外,我正在尝试找出一种方法,不仅可以在一个时间间隔内调用保存 function,而且可以在用户单击文本区域之外并失去焦点时调用。 I'm not sure how to do this since TinyMce changes it from a textarea to an iframe apparently.我不确定如何执行此操作,因为 TinyMce 显然将其从文本区域更改为 iframe。

Any help is greatly appreciated.任何帮助是极大的赞赏。

tinyMCE.editors will give you access to all of the editors on the page. tinyMCE.editors将使您能够访问页面上的所有编辑器。 Seehttp://www.tinymce.com/wiki.php/API3:property.tinymce.editors .请参阅http://www.tinymce.com/wiki.php/API3:property.tinymce.editors

So you can change your code to所以你可以改变你的代码

$(document).ready(function() {
    setInterval(function() { 
        for (edId in tinyMCE.editors)
            auto_save(edId);
    },30000);
});

This will save every editor on your page every 30 seconds though.不过,这将每 30 秒保存一次页面上的每个编辑器。 I'm not sure if this is what you want.我不确定这是否是您想要的。 There's also tinyMCE.activeEditor if you just want access to the currently active editor.如果您只想访问当前活动的编辑器,还有tinyMCE.activeEditor

In response to your questions below:在回答您的以下问题时:

1.You should be able to use the blur event for the text area to trigger your save. 1.您应该能够使用文本区域的模糊事件来触发您的保存。

$(document).ready(function() {
    for (edId in tinyMCE.editors) {
        $('#' + edId).blur(function() {
            auto_save($(this).attr('id'));
        });
    }
});

2.If you want access to the QuesID from inside your auto_save function, you can use 2.如果您想从auto_save function 中访问 QuesID,您可以使用

var quesId = $('#' + editor_id).attr('QuesID');

this is excellent.这太棒了。 I made a few changes because the post still was triggered multiple times.我做了一些更改,因为帖子仍然被多次触发。 Also, now the auto_save timer is reset when a change is made:此外,现在 auto_save 计时器在进行更改时重置:

$.status = function (message) {
    $('#statusMsg').html('<p>' + message + '</p>');
};
$.status('log div');

$(document).ready(function () {
var myinterval;    

//for version 4.1.5 
    tinymce.init({
        selector: 'textarea',
        width: "96%",
        height: "200",
        statusbar: true,
        convert_urls: false,
        plugins: [
            "advlist autolink lists charmap print preview",
            "searchreplace fullscreen",
            "insertdatetime paste autoresize"
        ],
        toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        external_plugins: {"nanospell": "/Scripts/nanospell/plugin.js"},
        nanospell_server: "asp.net", // choose "php" "asp" "asp.net" or "java"

        setup: function (ed) {  //start a 30 second timer when an edit is made do an auto-save 
            ed.on('change keyup', function (e) {
                //clear the autosave status message and reset the the timers
                $.status('');
                clearInterval(myinterval);
                myinterval = setInterval(function () {
                    for (edId in tinyMCE.editors)
                        auto_save(edId);
                }, 30000); //30 seconds
            });
        }
    });

    // Here is the auto_save() function that will be called every 30 secs
    function auto_save(editor_id) {
        var editor = tinyMCE.get(editor_id);
        if (editor.isDirty()) {
            var content = editor.getContent();
            content = content.replace("+", "%2B"); 
            content = content.replace("/", "%2F");
            $.ajax({
                type: "POST",
                url: "/PlanningReview/Save",
                data: "itemValue=" + content,
                cache: true,
                async: false,   //prevents mutliple posts during callback
                success: function (msg) {
                    $.status(msg)
                }
            });
        }
        else {
            return false;        // If nothing has changed, don't do anything
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM