最近的项目中遇到个很头疼的问题,异步提交表单后需要清空表单数据,项目中文本编辑器是ckeditor,用了好几种方式都无法将提交后的表单清空。
最后发现这种方式可以:
document.form1.reset();
CKEDITOR.instances.caseText.setData(' ');
开始采用jquery的 $("#caseText").val(''); 试试之后发现此方式不成功。
采用: document.form1.caseText.value='' 发现也是不可以。
采用:
var o = $("#caseText").cleditor()[0];//关键是这里获取第一个对象,否则upateFrame无法调用
$('#caseText').html(' ');
o.updateFrame();
document.form1.reset();
不但没有清除表单值反倒把原来清空的都填回去了。
具体说下我配置ckeditor.
$(function() {
CKEDITOR.editorConfig = function( config )
{/*
config.language = 'zh-cn';
config.toolbar = 'Background';
config.toolbar_Background = [
['Bold','Italic','Underline','Strike','-','Subscript','Superscript','RemoveFormat'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
['NumberedList','BulletedList','-','Outdent','Indent'],
'/',
['Styles','Format','Font','FontSize'],
['TextColor','BGColor'],
['Link','Unlink'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
['Maximize','ShowBlocks']
];
config.resize_enabled = false;
config.font_names = 'Arial;Verdana;Times New Roman;Georgia;Tahoma;宋体;黑体;微软雅黑';
config.disableObjectResizing = false;
*/
config.language = 'zh-cn';
config.toolbar = 'Background';
config.toolbar_Background = [
['Bold','Italic','Underline','Strike','-','Subscript','Superscript','RemoveFormat'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
'/',
['FontSize'],
['TextColor','BGColor'],
['Link','Unlink','Table'],
['NumberedList','BulletedList','-','Outdent','Indent'],
['BidiLtr','BidiRtl']
];
//config.entities = false;
config.resize_enabled = false;
config.disableObjectResizing = false;
config.filebrowserImageUploadUrl = 'ckupload?type=Images';
};
CKEDITOR.replace( 'caseText', { customConfig : 'config_front.js' }) ;
});
开始时候为了取值需要,将
var editor = CKEDITOR.replace( 'caseText', { customConfig : 'config_front.js' }) ;
设置成了全局变量,导致每次加载时候会报 存在实例错误。
为了避免类似错误,建议使用:
CKEDITOR.instances.caseText.setData(' ');
方式获取数据。