ck 编辑器 - 无法捕获提交;
我正在尝试使用 jquery 捕获表单提交,该表单提交是通过在 ck 编辑器中按“保存”提交的。
我的 javascript 代码是
$(function() {
var config = {
skin : 'office2003',
toolbar :[
['Save','Preview'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink'],
['Image','Table','HorizontalRule','SpecialChar','Iframe'],
['Format','Font','FontSize'],
['TextColor','BGColor', 'Bold','Italic','Underline','Strike','-','Subscript','Superscript']
]
};
$('#cont').ckeditor(config);
$('form').submit(function() {
var form = $(this);
var name = form.children('#name').val();
var desc = form.children('#desc').val();
var cont = form.children('#cont').val();
var id = form.children('#id').val();
$.ajax({
url: basePath + 'admin/ajax/pages/edit',
type: 'POST',
data: {
name: name,
desc: desc,
cont: cont,
id: id
},
success: function(data) {
if (data.response)
$('#ajaxSuccess').show('fast').delay(10000).hide('fast');
else
$('#ajaxError').show('fast').delay(10000).hide('fast');
},
error: function(data) {
$('#ajaxError').show('fast').delay(10000).hide('fast');
}
});
return false;
});
});
但由于某种原因,提交处理程序似乎没有被调用(通过 alert('used');
作为第一行进行测试),而是正常提交表单。
我做错了什么?
Based on the answer below I have updated my code to be
$(function() {
var saveCmd = {
modes : { wysiwyg:1, source:1 },
exec : function( editor ){
jQuery($form = editor.element.$.form).submit();
}
};
var pluginName = 'safesave';
// Register a plugin named "save".
CKEDITOR.plugins.add(pluginName, {
init : function( editor ){
var command = editor.addCommand( pluginName, saveCmd );
command.modes = { wysiwyg : !!( editor.element.$.form ) };
editor.ui.addButton( 'SafeSave',{
label : editor.lang.save,
command : pluginName,
className : 'cke_button_save'
});
}
});
var config = {
skin : 'office2003',
toolbar :[
['SafeSave','Preview'],
...
但现在我没有保存按钮,怎么了?
I am trying to catch a form submission with jquery that is submitted via pressing save in ck editor.
My javascript code is
$(function() {
var config = {
skin : 'office2003',
toolbar :[
['Save','Preview'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink'],
['Image','Table','HorizontalRule','SpecialChar','Iframe'],
['Format','Font','FontSize'],
['TextColor','BGColor', 'Bold','Italic','Underline','Strike','-','Subscript','Superscript']
]
};
$('#cont').ckeditor(config);
$('form').submit(function() {
var form = $(this);
var name = form.children('#name').val();
var desc = form.children('#desc').val();
var cont = form.children('#cont').val();
var id = form.children('#id').val();
$.ajax({
url: basePath + 'admin/ajax/pages/edit',
type: 'POST',
data: {
name: name,
desc: desc,
cont: cont,
id: id
},
success: function(data) {
if (data.response)
$('#ajaxSuccess').show('fast').delay(10000).hide('fast');
else
$('#ajaxError').show('fast').delay(10000).hide('fast');
},
error: function(data) {
$('#ajaxError').show('fast').delay(10000).hide('fast');
}
});
return false;
});
});
But for some reason the submit handler doesn't even seem to be called (tested via alert('called');
as first line), instead the form is submitted normally.
What am I doing wrong?
Based on the answer below I have updated my code to be
$(function() {
var saveCmd = {
modes : { wysiwyg:1, source:1 },
exec : function( editor ){
jQuery($form = editor.element.$.form).submit();
}
};
var pluginName = 'safesave';
// Register a plugin named "save".
CKEDITOR.plugins.add(pluginName, {
init : function( editor ){
var command = editor.addCommand( pluginName, saveCmd );
command.modes = { wysiwyg : !!( editor.element.$.form ) };
editor.ui.addButton( 'SafeSave',{
label : editor.lang.save,
command : pluginName,
className : 'cke_button_save'
});
}
});
var config = {
skin : 'office2003',
toolbar :[
['SafeSave','Preview'],
...
But now I have no save button, whats up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你不能。您需要一个不同的保存模块。我写这篇文章的目的正是为了这个:
现在只需将命令从
save
更改为SafeSave
即可。不知道为什么我称它为SafeSave
,也许我像现在一样累了:)请注意,这取决于 jQuery。如果您不使用 jQuery,请更改
exec
函数。Yeah, you can't. You need a different save module. I wrote this for this exact purpose:
Now just change your command from
save
toSafeSave
. Not sure why I called itSafeSave
, maybe I was tired like I am now :)Note that this depends on jQuery. If you aren't using jQuery, change the
exec
function.您可以使用 beforeCommandExec 事件 & cancel() 方法:
更新:
这在以下情况下不起作用CKEditor 版本 4.0、4.1、4.2,但自版本 4.3 起它再次可用。
从 CKEditor 版本 4.2 开始,您可以使用保存 事件,使用 cancel() 方法:
You can use the beforeCommandExec event & cancel() method:
Update:
This doesn't work in CKEditor versions 4.0, 4.1, 4.2, however it works again since version 4.3.
Since CKEditor version 4.2 you can use the save event with the cancel() method:
您可以使用 javascript 伪协议捕获提交:
...
You can catch the submit using the javascript pseudo-protocol:
...
如果您需要更新 ckeditor 上的元素,请使用以下代码:
If you need to update a element onchange ckeditor use this code: