2015-09-28 39 views
1

我們有一些代碼用於在模糊運行的CKEditor textarea上驗證輸入。我們一類的ckeditor_textarea添加到使用的CKEditor和運行該代碼添加必要的功能,模糊事件的所有文字區域:如何觸發CKEditor textareas上的模糊事件?

$("textarea.ckeditor_textarea").each(function(){ 
    var textarea_id = $(this).attr("id"); 
    CKEDITOR.instances[textarea_id].on('blur',function(){ 
     // Validation functions here 
    }); 
}); 

這個工作消防驗證功能時,模糊事件發生。但在提交之前,按下提交按鈕以在CKEditor textareas上運行驗證功能時,我們還需要手動觸發blur事件。

如何在CKEditor textarea上觸發模糊事件?使用jQuery語法(這當然不行,因爲CKEditor的實例不是一個jQuery對象),我基本上在尋找這樣的事情:

$("textarea.ckeditor_textarea").each(function(){ 
    var textarea_id = $(this).attr("id"); 
    CKEDITOR.instances[textarea_id].trigger('blur'); 
}); 

回答

1

對於提交驗證我會建議使用updateElement()方法您提交處理程序中,然後運行驗證代碼:

以下將更新所有並使用頁面上編輯的所有元素:

$('form').on('submit', function(e){ 
    for (instance in CKEDITOR.instances) { 
     CKEDITOR.instances[instance].updateElement(); 
     } 
     // run validation code 

}); 

這也可以確保表單數據是最新的,與編輯他們自己

+0

增加投票,因爲這可能是在某些情況下的一個很好的解決方案,'updateElement()'是一個好點。雖然這並不是我想要的,但它足以讓我解決我的情況。 (已發佈答案) –

0

基於@ charlietfl的回答,我能夠爲我的情況想出一個解決方案。

首先,我創建了一個函數來調用運行驗證碼:

function ckeditor_blur_event(textarea_id){ 
    // validation code 
} 

然後,我改變了一個代碼塊在我的問題,以使用新的功能:

$("textarea.ckeditor_textarea").each(function(){ 
    var textarea_id = $(this).attr("id"); 
    ckeditor_blur_event(textarea_id) 
}); 

最後,當提交表格時,我觸發相同的驗證碼:

$("textarea.ckeditor_textarea").each(function(){ 
    var textarea_id = $(this).attr("id"); 
    CKEDITOR.instances[textarea_id].updateElement(); 
    ckeditor_blur_event(textarea_id) 
}); 
2
  1. 你不應該混合使用jQuery事件和CKEDITOR事件。如果你想有CKEDITOR例如模糊,註冊它:

    ckeInst.on( '模糊',函數(E){

    });

然後,如果你真的想要觸發blur事件,你做這樣的:

ckeInst.focusManager.blur(true); 

編輯從事件retrived(如果有的話),或者通過CKEDITOR.instances [ 'yourCkeName'];

相關問題