简体   繁体   English

如何使用ckeditor通过Ajax提交textarea数据?

[英]How can I use ckeditor to submit textarea data with ajax?

Ok so this I what I'm working with. 好的,这就是我正在使用的。 This is the code for the textarea being affected: 这是受影响的文本区域的代码:

<div class="chat">
    <div class="messages"></div>
    <textarea class="entry" name="entry" placeholder="Welcome to the Chat. Enter your message here!"></textarea>
    <script type="text/javascript">
        editor = CKEDITOR.replace('entry');
    </script>
</div>

This is the code for my chat.js file that calls all the functions for the chat: 这是我的chat.js文件的代码,该文件调用了聊天的所有功能:

var chat = {}

chat.fetchMessages = function () {
    $.ajax({
        url: 'ajaxchat.php',
        type: 'post',
        data: {
            method: 'fetch'
        },
        success: function (data) {
            $('.chat .messages').html(data);
        }
    });
}

chat.throwMessage = function (message) {
    if ($.trim(message).length != 0) {
        $.ajax({
            url: 'ajaxchat.php',
            type: 'post',
            data: {
                method: 'throw',
                message: message
            },
            success: function (data) {
                chat.fetchMessages();
                chat.entry.val('');
            }
        });
    }
}

chat.entry = $('.chat .entry');
chat.entry.bind('keydown', function (e) {
    if (e.keyCode === 13 && e.shiftKey === false) {
        chat.throwMessage($(this).val());
        e.preventDefault();
    }
});

chat.interval = setInterval(chat.fetchMessages, 5000)
chat.fetchMessages();

Right now all this code is working but the return key isnt being caught to submit the message. 现在,所有这些代码都可以正常工作,但是没有捕获返回键来提交消息。 Not sure what else I need to do. 不知道我还需要做什么。 CKeditor is interpreting the enter/return key as a new paragraph and I need it to submit message data. CKeditor将输入/返回键解释为新段落,我需要它来提交消息数据。

If I understood the problem correctly, your issue is with events. 如果我正确理解了问题,那么您的问题是事件。 You don't bind to events like that for CKEditor. 您不会绑定到CKEditor这样的事件。 It doesn't use the textarea element directly, it does some magic tricks and creates an iframe and has its own event handling system. 它不直接使用textarea元素,它做了一些魔术,并创建了iframe并拥有自己的事件处理系统。 To attach to the 13 key event, do something like the following 要附加到13键事件,请执行以下操作

CKEDITOR.on('instanceReady', function(ev) {
    var editor = ev.editor;

    editor.on('key', function(evt) {
        if (evt.data.keyCode === 13) {
            // do your enter handling here
        }
    }
}

On a related note, getting the value like that is a little strange too. 与此相关的是,获得这样的值也有点奇怪。 Does it actually work? 它真的有效吗? I would change that for the more CKEditor-like 我将其更改为更类似于CKEditor的形式

CKEDITOR.instances.entry.getData();

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

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