我有两个 CKEditors 在我的 HTML(我的意思是说多个 ckeditors)。
另外我使用验证插件检查如果 CKEditor 是空的,如果空显示错误。
验证工作完美,但它验证第二次,而它应该验证第一次本身。
我已经检查了所有的问题和答案在这里,但没有帮助。
我创建了一个JS Fiddle。
用于验证的代码:
HTML格式
<form action="" method="post" id="frmEditor">
<p>
<label for="editor1">
Editor 1:
</label>
<textarea cl="ckeditor" cols="80" id="editor1" name="editor1" rows="10"></textarea>
</p>
<p>
</p>
<p>
<label for="editor1">
Editor 2:
</label>
<textarea cl="ckeditor" cols="80" id="editor2" name="editor2" rows="10"></textarea>
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
剧本
$(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();
}
});
});
您需要在验证执行之前更新实例。所以首先在按钮上添加一个id属性或类
<input type="submit" value="Submit" id="submitFormBtn">
现在在你的 js 代码下面写函数
$('#submitFormBtn').click(function () {
CKEDITOR.instances.editor1.updateElement();
CKEDITOR.instances.editor2.updateElement();
});
希望这将工作。
jQuery.validator.setDefaults({
ignore: [],
// with this no hidden fields will be ignored E.g. ckEditor text-area
});
我观察到验证工作在第二次提交。原因是,ckEditor
隐藏实际的文本区域并将 iframe 作为编辑器实例,并在提交时将内容推送到文本区域。这意味着,TextArea
上的验证在陈旧数据上被触发。为了解决这个问题,我正在更新编辑器实例的文本更改上的TextArea
。
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].on('change', function ()
{
var editorName = $(this)[0].name;
CKEDITOR.instances[editorName].updateElement();
});
}
我试过以下解决方案,它的工作。
<textarea name="txtDesc<?php echo $i;?>" id="txtDesc<?php echo $i;?>" cl="ckeditor" rows="5" cols="17" ><?php echo $txtDesc?></textarea>
<script>
CKEDITOR.replace("txtDesc<?php echo $i;?>");
CKEDITOR.add
//CKEDITOR.replace('txtDesc0');
</script>
如果只有 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(); });
});
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(59条)