繁体   English   中英

使用CKEditor + jQuery Validation更新textarea

[英]Update textarea with CKEditor + jQuery Validation

我正在尝试验证包含CKEditor支持的textarea的表单。 但是现在,它根本不显示任何错误,也没有提交代码。

我已经阅读了与该主题相关的所有答案,但我的代码无法正常工作。

到目前为止,这是我得到的:

if(jQuery().validate) {
    $("#submit_button").click(function(e){
        e.preventDefault();
        CKEDITOR.instances.editor1.updateElement();

        $(" #content_form ").validate({
            ignore: "input:hidden:not(input:hidden.required),input:hidden:not(input:hidden.editor1)",
            errorPlacement: function(error, element) {
                if (element.attr("name") == "editor1")
                {
                    error.insertBefore("#editor1");
                }
                else
                {
                    error.insertAfter(element);
                }
            },
            rules: {
                title: {
                    required: true,
                    minlength: 5
                },
                editor1: {
                    required: true,
                    minlength: 10
                },
                imglink: {
                    url:true
                },
            },
            messages: {
                title: "The title field is required.",
                editor1: "The content field is required.",
                imglink: "You must provide a valid URL."
            }
        });
    });
}

如果您能帮助解决这个问题,我将不胜感激。

提前致谢。

我正在做的事情是这样的:

CKEDITOR.instances['txtSomeTextbox'].updateElement(); // this will update text in the hidden textbox that CKE is using.

// or if you have multiple instances 
// for (var i in CKEDITOR.instances) {
//CKEDITOR.instances[i].updateElement(); // to update the textarea
//}

然后,我得到文本框的值,如下所示:

var mytext = CKEDITOR.instances.txtSomeTextbox.getData();

您现在可以使用mytext进行任何验证。

您的问题之一是对.validate()方法的基本误解。

您的代码...

$("#submit_button").click(function(e){
    ....

    $(" #content_form ").validate({
        ....

由于.validate()是插件的初始化方法,因此通常在DOM ready事件中调用一次 它不属于提交按钮的click处理程序事件的内部,因为插件需要在单击事件之前进行初始化。 它不会被多次调用,因为所有后续调用都会被忽略。 )此外,该插件捕获各种click事件并根据需要自动执行任何已定义的验证。

$(document).ready(function() { // <-- DOM Ready event
    ....

    $("#content_form").validate({  // <-- Initialize the plugin on this form
        ....

插件的基本演示: http : //jsfiddle.net/EN3Yb/

暂无
暂无

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

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