之前有朋友遇到過這個問題,就是KindEditor在火狐下或者其餘瀏覽器下都沒法獲得textarea文本框的值,點擊表單提交按鈕獲得的是空白。昨每天涯PHP博客[http://blog.phpha.com]也無心遇到這個問題,因而想認真找下緣由。
首先描述下我這邊KindEditor的錯誤現象:
一、在IE8/FF下均得不到值;
二、當點擊KindEditor的全拼按鈕切換到全屏模式輸入時,再返回正常模式,能夠獲得值;
三、我用的是jQuery的點擊事件提交表單的,提交,沒法獲得值;
四、直接用表單的提交按鈕能夠獲得值。javascript
下面以 KindEditor 4.x 版本爲例說明,先貼上正確的代碼:php
<script type="text/javascript"> //天涯PHP博客 http://blog.phpha.com KindEditor.ready(function(K){ K.create('textarea[name="content"]', { themeType: 'simple', resizeType: 1, uploadJson: 'common/KEditor/upload_json.php', fileManagerJson: 'common/KEditor/file_manager_json.php', allowFileManager: true, //經測試,下面這行代碼無關緊要,不影響獲取textarea的值 //afterCreate: function(){this.sync();} //下面這行代碼就是關鍵的所在,當失去焦點時執行 this.sync(); afterBlur: function(){this.sync();} }); }); </script>
相關說明:html
從上面的代碼能夠看到,解決方法在於最後一行代碼,afterBlur: function(){this.sync();},當失去焦點時執行 this.sync();
那麼這個 this.sync(); 函數是幹嗎的呢?簡單的說:這個函數就是同步KindEditor的值到textarea文本框。
官方解釋:
//天涯PHP博客 http://blog.phpha.com
sync()
將編輯器的內容設置到原來的textarea控件裏。
參數: 無
返回: KEditor
地址:http://www.kindsoft.net/docs/editor.html#syncjava
補充:在 KindEditor 4.x 版本中,KE.sync(); 要改爲 this.sync();json