jQuery 验证插件 + CKEditor - 键入时验证

发布于 2024-08-14 18:36:54 字数 1967 浏览 2 评论 0原文

我在文本区域和 jQuery 验证插件上使用 CKEditor (http://bassistance .de/jquery-plugins/jquery-plugin-validation/)

使用 jQuery 插件,可以将字段标记为空或必填。 当“产品名称”等字段为空时,提交表单时该字段将被标记为无效。

但是,在输入“产品名称”时,该字段将在输入时自动标记为有效。太棒了,但我希望我的启用 CKEditor 的文本区域也能有同样的效果。在纯文本区域中一切都很好(当输入文本时,该字段变得有效),但是在将 CKEditor 添加到文本区域后,这不再起作用。

从那时起,您必须在 CKEditor 文本区域重新验证之前单击提交按钮。

关于如何使这项工作有效的任何想法?

更新:

我尝试了您给我的线程中的解决方案,但它不起作用: 使用 jQuery 从 CKEditor 的 iframe 中抓取内容

I有:

CKEDITOR.instances["page_content"].document.on('keydown', function(event)
                {
                    CKEDITOR.tools.setTimeout( function()
                    { 
                        $("#page_content").val(CKEDITOR.instances.page_content.getData()); 
                    }, 0);
        });

但这一直给我:“CKEDITOR.instances.page_content.document未定义”

我的文本的id是“page_content”

单击按钮后效果很好,但正如你所见,我需要以某种方式触发keydown事件

$("#btnOk").click(function(event) {
            CKEDITOR.instances.page_content.updateElement(); //works fine
});

更新2:

这段代码是正确的,它从 CKEDITOR 获取数据到我的文本区域,但是我的验证插件仍然没有发生任何事情,它没有在 CKEDitor 中“在我键入时做出响应”,而它应该做出反应并说该字段正常当我输入一些文字时

CKEDITOR.instances["page_content"].on("instanceReady", function()
        {
                //set keyup event
                this.document.on("keyup", updateTextArea);
                 //and paste event
                this.document.on("paste", updateTextArea);
                
        });

        function updateTextArea()
        {
            CKEDITOR.tools.setTimeout( function()
                    { 
                        $("#page_content").val(CKEDITOR.instances.page_content.getData());
                    }, 0);  
        }

I'm using CKEditor on a textarea and the jQuery validation plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

With the jQuery plugin it's possible to mark a field as empty or required.
When a field e.g. "product name" is empty the field will be marked as invalid when submitting the form.

But, while typing in a "product name", the field will be automatically marked as valid while typing. That's great, but I want to have the same with my CKEditor-enabled textarea. Everything works great with a plain textarea (when typing text, the field becomes valid), but this doesn't work anymore after adding CKEditor to the textarea.

From then on, you have to click the submit button before the CKEditor textarea gets re-validated.

Any ideas on how I can make this work?

UPDATE:

I tried the solution from the thread you gave me but it doesn't work:
Using jQuery to grab the content from CKEditor's iframe

I have:

CKEDITOR.instances["page_content"].document.on('keydown', function(event)
                {
                    CKEDITOR.tools.setTimeout( function()
                    { 
                        $("#page_content").val(CKEDITOR.instances.page_content.getData()); 
                    }, 0);
        });

but that keeps giving me: "CKEDITOR.instances.page_content.document is undefined"

The id of my textare is "page_content"

This works fine after clicking on the button, but as you see I need to trigger the keydown event somehow

$("#btnOk").click(function(event) {
            CKEDITOR.instances.page_content.updateElement(); //works fine
});

UPDATE 2:

This code is correct and it gets the data from CKEDITOR into my textarea, but still, nothing is happening with my validation plugin, it's not "responding as I type" in the CKEDitor while it should react and say that the field is OK when I type in some text

CKEDITOR.instances["page_content"].on("instanceReady", function()
        {
                //set keyup event
                this.document.on("keyup", updateTextArea);
                 //and paste event
                this.document.on("paste", updateTextArea);
                
        });

        function updateTextArea()
        {
            CKEDITOR.tools.setTimeout( function()
                    { 
                        $("#page_content").val(CKEDITOR.instances.page_content.getData());
                    }, 0);  
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-08-21 18:36:54

jquery 验证插件对文本区域或文本字段中的 keyup 事件做出反应,因此您需要在更新文本区域后触发该事件。

看看这个:

    CKEDITOR.instances["page_content"].on("instanceReady", function()
    {
            //set keyup event
            this.document.on("keyup", updateTextArea);
             //and paste event
            this.document.on("paste", updateTextArea);
            
    });

    function updateTextArea()
    {
        CKEDITOR.tools.setTimeout( function()
                { 
                    $("#page_content").val(CKEDITOR.instances.page_content.getData());
                    $("#page_content").trigger('keyup');
                }, 0);  
    }

The jquery validation plugin is reacting to the keyup event in the textarea or text fields, so you need to trigger that event after updating your textarea.

Check this out:

    CKEDITOR.instances["page_content"].on("instanceReady", function()
    {
            //set keyup event
            this.document.on("keyup", updateTextArea);
             //and paste event
            this.document.on("paste", updateTextArea);
            
    });

    function updateTextArea()
    {
        CKEDITOR.tools.setTimeout( function()
                { 
                    $("#page_content").val(CKEDITOR.instances.page_content.getData());
                    $("#page_content").trigger('keyup');
                }, 0);  
    }
静谧 2024-08-21 18:36:54

您必须调整 CKEditor 文本区域以在失去焦点时写回 HTML,而不是在提交表单时写回 HTML(我认为这是默认情况下的做法)。

几天前有人提出了类似的问题,似乎已经成功,

You would have to tweak the CKEditor textarea to write back the HTML when it loses focus, instead of (I assume this is what it does by default) when the form is submitted.

Somebody had a similar question a few days ago and seems to have been successful, check it out.

无所谓啦 2024-08-21 18:36:54

无需做任何事情。

第一步:重要:在提交之前用实际内容更新 CKEDITOR textarea

form.on('submit', function () {
        for (var instanceName in CKEDITOR.instances) {
            CKEDITOR.instances[instanceName].updateElement();
        }
    });

下一步:然后验证您的代码(必须是最后一步)

form.validate();

No need to do anything.

1st STEP: IMPORTANT: update CKEDITOR textarea with actual content before submit

form.on('submit', function () {
        for (var instanceName in CKEDITOR.instances) {
            CKEDITOR.instances[instanceName].updateElement();
        }
    });

Next STEP: Then after validate your code (Must be last step)

form.validate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文