[英]using CKEditor with jQuery validation plugin not working
我正在使用來自基礎的CKeditor和jQuery驗證插件。 我的textarea(上面有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
謝謝czarchaic,
我通過寫來解決:
CKEDITOR.instances.prod_description.updateElement();
其中“ prod_description”是您的文本名稱,其中鏈接了CKeditor。
這可能不是最好的方法,但是我創建了一個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"
我的頁面上有多個CKEDITORS,所以我這樣做:
// 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.