之前有朋友遇到过这个问题,就是KindEditor在火狐下或者其余浏览器下都没法获得textarea文本框的值,点击表单提交按钮获得的是空白。昨每天涯PHP博客[http://blog.phpha.com]也无心遇到这个问题,因而想认真找下缘由。
首先描述下我这边KindEditor的错误现象:
一、在IE8/FF下均得不到值;
二、当点击KindEditor的全拼按钮切换到全屏模式输入时,再返回正常模式,能够获得值;
三、我用的是jQuery的点击事件提交表单的,提交,没法获得值;
四、直接用表单的提交按钮能够获得值。javascript
下面以 KindEditor 4.x 版本为例说明,先贴上正确的代码:php
1html 2java 3json 4浏览器 5编辑器 6函数 7测试 8this 9 10 11 12 13 14 15 16 |
<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> |
相关说明:
从上面的代码能够看到,解决方法在于最后一行代码,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#sync
补充:在 KindEditor 4.x 版本中,KE.sync(); 要改为 this.sync();