ckeditor数据未通过jQuery验证进行验证

 ougq 发布于 2023-02-08 12:21

我知道那里有很多关于这个的问题,但我不能让这个为我的生活工作.我已经尝试了几个解决方案,包括这个,第二个答案在这里和这个,我无法得到"必需"的消息出现.当ckeditor字段为空时,表单仍然会提交.

我查看了这里的文档,并且能够将编辑器的内容传递给警报,但是我没有足够的经验知道如何将它与验证插件集成.我花了这么多时间在这上面 - 有人可以帮忙吗?

这是我目前的代码,我创建了一个小提琴:http://jsfiddle.net/BmZ93/1/

     $('#add-job').validate({
                rules: {
                editor1: {
                    required: function() 
                    {
                    CKEDITOR.instances.editor1.updateElement();
                    }
                    }
                },
                messages: {
                Job_Title: "Required",
                Job_Location: "Required",
                jobid: "Required",
                Job_Cat: "Required",
                editor1: "Required"
                } 
        });

Rohan Patil.. 33

这是用这个更新你的代码

http://jsfiddle.net/rohanppatil/BmZ93/8/

$(document).ready(function() {
$('#add-job').validate({
    ignore: [],         
    rules: {
                editor1: {
                    required: function() 
                    {
                    CKEDITOR.instances.editor1.updateElement();
                    }
                    }
                },
                messages: {
                Job_Title: "Required",
                Job_Location: "Required",
                jobid: "Required",
                Job_Cat: "Required",
                editor1: "Required"
                },
                /* use below section if required to place the error*/
                errorPlacement: function(error, element) 
                {
                    if (element.attr("name") == "editor1") 
                   {
                    error.insertBefore("textarea#editor1");
                    } else {
                    error.insertBefore(element);
                    }
                }
            });
});

希望这会工作我在JSFIDDLE中测试这个工作正常

2 个回答
  • 您应该更改ignore默认保存:hidden值的属性值.作为CKEDITOR隐藏textarea的jQuery验证不验证元件:

    ignore: [] 
    

    http://jsfiddle.net/BmZ93/5/

    另请注意,您应该在required方法中返回一个布尔值,而不是.这里通过true就足够了.

    required: true
    

    这是更新的代码,它也使用errorPlacement方法为CKEDITOR包装器添加边框,您可以根据需要自定义它:

    $(document).ready(function () {
        $('#add-job').validate({
            rules: {
                'editor1': {
                    required: true
                }
            },
            messages: {
                Job_Title: "Required",
                Job_Location: "Required",
                jobid: "Required",
                Job_Cat: "Required",
                editor1: "Required"
            },
            errorPlacement: function(error, $elem) {
                if ($elem.is('textarea')) {
                    $elem.next().css('border', '1px solid red');
                }
            },
            ignore: []
        });
    });
    

    2023-02-08 12:22 回答
  • 这是用这个更新你的代码

    http://jsfiddle.net/rohanppatil/BmZ93/8/

    $(document).ready(function() {
    $('#add-job').validate({
        ignore: [],         
        rules: {
                    editor1: {
                        required: function() 
                        {
                        CKEDITOR.instances.editor1.updateElement();
                        }
                        }
                    },
                    messages: {
                    Job_Title: "Required",
                    Job_Location: "Required",
                    jobid: "Required",
                    Job_Cat: "Required",
                    editor1: "Required"
                    },
                    /* use below section if required to place the error*/
                    errorPlacement: function(error, element) 
                    {
                        if (element.attr("name") == "editor1") 
                       {
                        error.insertBefore("textarea#editor1");
                        } else {
                        error.insertBefore(element);
                        }
                    }
                });
    });
    

    希望这会工作我在JSFIDDLE中测试这个工作正常

    2023-02-08 12:22 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有