CK编辑第二次validation

我的HTML中有两个CKEditor(我的意思是说多个ckeditors)。

另外我使用Validate插件检查CKEditor是否为空,如果为空显示错误。

validation工作完美,但它第二次validation,而它应该第一次validation自己。

我在这里检查了所有的问题和答案,但都没有帮助。

我创建了一个JS小提琴 。

validation代码:

HTML

脚本

 $(document).ready(function(){ // validate signup form on keyup and submit $("#frmEditor").validate({ ignore: [], debug: false, rules: { editor1:{ required: true }, editor2:{ required: true } }, messages: { editor1: { required: "Please enter" }, editor2: { required: "Please enter" } }, submitHandler: function(form) { form.submit(); } }); }); 

您需要在validation执行之前更新实例。 所以首先在按钮上添加一个id属性或类

  

现在在你的js代码中写下面的函数

 $('#submitFormBtn').click(function () { CKEDITOR.instances.editor1.updateElement(); CKEDITOR.instances.editor2.updateElement(); }); 

希望这会奏效。

 jQuery.validator.setDefaults({ ignore: [], // with this no hidden fields will be ignored Eg ckEditor text-area }); 

我观察到validation正在进行第二次提交。 原因是, ckEditor隐藏了实际的文本区域并将iframe作为编辑器实例,并在提交时将内容推送到文本区域。 这意味着, TextArea上的validation会被陈旧数据触发。 要解决此问题,我正在更新编辑器实例的文本更改我的TextArea

  for (instance in CKEDITOR.instances) { CKEDITOR.instances[instance].on('change', function () { var editorName = $(this)[0].name; CKEDITOR.instances[editorName].updateElement(); }); } 

我尝试了以下解决方案并且有效。

    

如果只有1个ckEditor

 for (instance in CKEDITOR.instances) { CKEDITOR.instances[instance].on("instanceReady", function () { //set keyup event this.document.on("keyup", function () { CKEDITOR.instances[instance].updateElement(); }); //and paste event this.document.on("paste", function () { CKEDITOR.instances[instance].updateElement(); }); }); } 

如果有超过1个ckEditor(在我的情况下为2)

 CKEDITOR.instances["txtDesc0"].on("instanceReady", function () { this.document.on("keyup", function () { CKEDITOR.instances["txtDesc0"].updateElement(); }); this.document.on("paste", function () { CKEDITOR.instances["txtDesc0"].updateElement(); }); this.document.on("cut", function () { CKEDITOR.instances["txtDesc0"].updateElement(); }); }); CKEDITOR.instances["txtDesc1"].on("instanceReady", function () { this.document.on("keyup", function () { CKEDITOR.instances["txtDesc1"].updateElement(); }); this.document.on("paste", function () { CKEDITOR.instances["txtDesc1"].updateElement(); }); this.document.on("cut", function () { CKEDITOR.instances["txtDesc1"].updateElement(); }); });