使用 CKEditor 和 jQuery 验证插件不起作用

发布于 2024-08-15 15:27:20 字数 841 浏览 1 评论 0原文

我正在使用 CKeditor 和 basicance 的 jQuery 验证插件。我的文本区域(上面有 CKEditor)正在通过 jQuery 进行验证,但只有在第二次单击提交按钮后才有效。

简而言之: 当我第一次在 CKEditor 中输入数据时提交表单,它显示“字段为空”。第二次就说没问题,正在提交表格。

我读到了一个解决方案:

“你可以通过在每个验证例程之前调用 CKEDITOR.editor::updateElement 来解决这个问题。”

但我找不到如何实现它:

$(document).ready(function(){
    CKEDITOR.replace( 'prod_description',
    {
        toolbar: 'MyToolbar'
    }
    );

    $("#btnOk").click(function(){
        CKEDITOR.instances.editor1.updateElement();
        alert('click');
    });
});

这总是给我错误:“CKEDITOR.instances.editor1 未定义”

有关如何解决此问题的任何想法。 CKEditor 的文档在这里: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR。 editor.html#updateElement

I'm using CKeditor and the jQuery validation plugin from basistance. My textarea (with the CKEditor on it) is being validated by jQuery, but that only works after the second click on my submit button.

In short:
the first time I submit the form when data is entered in the CKEditor, it says "field is empty". The second time it says it's ok and the form is being submitted.

I read a solution for this:

"you could work around this problem by calling CKEDITOR.editor::updateElement right before every validation routine."

I cannot find how to implement it though:

$(document).ready(function(){
    CKEDITOR.replace( 'prod_description',
    {
        toolbar: 'MyToolbar'
    }
    );

    $("#btnOk").click(function(){
        CKEDITOR.instances.editor1.updateElement();
        alert('click');
    });
});

This always gives me the error: "CKEDITOR.instances.editor1 is undefined"

Any ideas on how to solve this. Documentation from CKEditor is here:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#updateElement

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

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

发布评论

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

评论(3

莫言歌 2024-08-22 15:27:20

我通过编写以下内容解决了这个问题:

CKEDITOR.instances.prod_description.updateElement();

其中“prod_description”是链接到 CKeditor 的文本区域的名称。

I solved it by writing:

CKEDITOR.instances.prod_description.updateElement();

where "prod_description" is the name of your textarea with CKeditor linked to it.

烂人 2024-08-22 15:27:20

这可能不是最好的方法,但我创建了一个 jquery 规则,它只是检查 CKEditor 中是否有数据。如果有数据,那么它就通过了规则。如果没有,那么它就失败了。这似乎对我很有效。

//Have to build custom method to check ckeditor
jQuery.validator.addMethod("ckeditor", function(value, element) { 
    var textData = editor.getData();
    if(textData.length>0) return true;
    return false;
}, "No data in editor");

然后,我的规则如下所示:

'fieldName': "ckeditor"

This may not be the best way to do it, but I created a jquery rule that simply checks to see if there is data in the CKEditor. If there is data, then it passes the rule. If not, then it fails. This seems to work for me pretty well.

//Have to build custom method to check ckeditor
jQuery.validator.addMethod("ckeditor", function(value, element) { 
    var textData = editor.getData();
    if(textData.length>0) return true;
    return false;
}, "No data in editor");

Then, my rule looks like this:

'fieldName': "ckeditor"
酒与心事 2024-08-22 15:27:20

我的页面上有多个 CKEDITOR,所以我这样做:

        // You need to update the editors before jqValidator.
        for (var i in CKEDITOR.instances)
        {
            CKEDITOR.instances[i].updateElement();
        }
        if (!jqValidator.form())
        {
            alert('Error Alert: please correct the errors detailed below, then click "Apply changes" again.');
            return;
        }

I have multiple CKEDITORS on my page, so I do this:

        // You need to update the editors before jqValidator.
        for (var i in CKEDITOR.instances)
        {
            CKEDITOR.instances[i].updateElement();
        }
        if (!jqValidator.form())
        {
            alert('Error Alert: please correct the errors detailed below, then click "Apply changes" again.');
            return;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文