使用 Jquery 检测来自 CKEditor 的 onChange 事件
我正在使用 CKEditor 和 jQuery,每当用户更改字段的值时,我想将标志切换为 true。这些字段之一是 CKEditor 实例。
所有具有“wysiwyg”类的文本区域都会转换为 CKEditors,但不知为何 $('.wysiwyg').change()
事件永远不会被检测到。我做了一些谷歌搜索,但关键字组合似乎只带来了不相关的结果(我的谷歌很糟糕)。
感谢您的帮助:)
编辑:
for (var i in CKEDITOR.instances) {
CKEDITOR.instances[i].on('click', function() {alert('test 1 2 3')});
}
我尝试了上面的代码,但它不起作用。它没有给我一个错误,这意味着它找到了 CKEditor 对象,但由于某种原因监听器没有附加到它?
另外,如果我用 alert(CKEDITOR.instances[i].name);
替换事件附件,它会提醒我的文本区域的名称,这样我就知道我没有尝试附加点击事件归于无事:)
I'm working with the CKEditor and jQuery and I'd like to toggle a flag to true whenever a user changes the value of a field. One of those fields is a CKEditor instance.
All the textareas that have the "wysiwyg" class get converted to CKEditors but somehow the $('.wysiwyg').change()
event never gets detected. I did some googling but the keyword combination seems to bring up nothing but irrelevant results (my google-fu sucks).
Thanks for any help :)
Edit:
for (var i in CKEDITOR.instances) {
CKEDITOR.instances[i].on('click', function() {alert('test 1 2 3')});
}
I tried the code above and it doesn't work. It doesn't give me an error meaning that it finds the CKEditor objects but for some reason the listener isn't attached to it?
Also, if I replace the event attachment with just alert(CKEDITOR.instances[i].name);
it'll alert the name of my textarea so I know I'm not trying to attach the click event to nothing :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以在这篇文章中获得一个插件(以及有关检测到哪些内容发生更改的说明):http://alfonsoml.blogspot.com/2011/03/onchange-event-for-ckeditor.html 这样你就可以做类似的事情
You can get a plugin (and an explanation about what things are detected as changes) in this post: http://alfonsoml.blogspot.com/2011/03/onchange-event-for-ckeditor.html so you can do things like
我还没有成功地让 onchange 工作,但是 onblur 似乎可以工作,这可能足以满足您的需求
I haven't managed to get onchange working however onblur seem to work which may be sufficient for your needs
现在有一个更改事件: http://docs.ckeditor.com/#!/api /CKEDITOR.editor-event-change
以下内容适用于我使用的版本 4.4.3。当源代码和 HTML 发生变化时,您需要进行处理。
There is now a change event: http://docs.ckeditor.com/#!/api/CKEDITOR.editor-event-change
The following is working for me using version 4.4.3. You need to handle when the Source and HTML changes.
我明白了!
首先,我使用 ckeditor 类将网站上的编辑器创建为文本区域,然后让 ckeditor.js 将它们设为 richTextEditors。然后我尝试了与其他答案不同的解决方案,但无法使它们发挥作用。
然后我更改了类名称(更改为 CKeditor),因此我必须在代码中初始化编辑器。我尝试了这个,它起作用了:
因此,当编辑器失去焦点时,它会检查它是否脏,如果是,则触发更改后的函数(本例中的警报)。
后来我再次尝试使用 onchanges 插件,以这种方式:
现在它也可以与该插件一起使用(感谢@Alfonso)。但我认为该插件使更改事件触发过多,我更喜欢第一个解决方案,其中更改仅在模糊时进行评估。
希望这有帮助!感谢大家的帮助。
I got it!
First I had the editors on my web created as textareas with the class ckeditor, and I let the ckeditor.js make them richTextEditors. Then I tried distinct solutions from the other answers, but couldn't make them work.
Then I changed the class name (to CKeditor), so I have to initialize the editors in my code. I tried this and it works:
So when a editor loses the focus it checks if it's dirty, and if it is then it fires the changed function (the alert in this example).
Later I've tried again with the onchanges plugin, in this way:
Now it works with the plugin too (thanks @Alfonso). But I think that the plugin makes the changes event fire too much, I prefer the first solution where changes are only evaluated on blur.
Hope this helps! and thanks to all for your help.
CKEditor 有一个
'checkDirty()'
函数。检查 CKEditor 值是否已更改的最简单方法是在代码中添加这段 JS。注意:
emailBody
- 是文本区域字段的 IDThe CKEditor has function a
'checkDirty()'
. The simplest way to check if the CKEditor value has been changed is to add this piece of JS in your Code.Note:
emailBody
- is the id of your textarea field我能做的,基本上就是告诉您编辑器的内容是否已从原始状态更改。
Best I can do, basically gives you an indication as to if the content of the editor has changed from the original state.
我还没有想出解决方案,但以下链接详细介绍了 CKEditor 可用的事件:
http://alfonsoml.blogspot.com/2009/09/ckeditor-events.html
这不是我一直在寻找的完美解决方案,但我使用以下内容来标记我的内容已修改:
这实际上并不意味着内容已被修改,而是用户已从编辑器中聚焦或模糊(这比没有好)。
I haven't come up with a solution but the following link talks more about the events available witht he CKEditor:
http://alfonsoml.blogspot.com/2009/09/ckeditor-events.html
This is not the perfect solution I was looking for but I'm using the following to flag that my content has been modified:
This doesn't actually mean the content has been modified but that the user has focused or blurred from the editor (which is better than nothing).
对于事件更改,下载插件 onchange http://ckeditor.com/addon/onchange 并执行此操作
For event change download the plugin onchange http://ckeditor.com/addon/onchange and do this
ETC
etc
从我在 CKEditor 文档网站上看到的内容来看,CKEditor 带有内置的事件处理功能。这意味着您应该使用它而不是 JQuery 来监听 onChange 事件。这也主要是因为当像 CKEditors 这样的编辑器创建他们的“花哨”用户界面时,您最终会听到错误的元素进行更改。我相信如果您可以获得 CKEditor javascript 对象的引用,您应该能够编写如下内容:
我希望这会有所帮助。
from what I can see on the CKEditor documentation site CKEditor comes with built event handling. This means that you should use that instead of JQuery to listen to the onChange event. This is also mainly because when editors like CKEditors create their "fancy" uis you will end up listening to the wrong element for a change. I believe if you can get the reference of the CKEditor javascript object you should be able to write something like this:
I hope this helps.
此代码将提醒用户单击任何侧边栏链接
This code will alert user on clicking any sidebar link
我显然不能简单地添加评论,因为我是该网站的新手,但加里的答案是正确的,并且我已经独立验证了它,所以我想添加它。
CKEDITOR.instances[xxx].on('更改', function() {alert('值更改!!')});
这适用于 CKEditor 4+,并捕获所有更改事件,包括撤消和重做,使新用户不需要 Alfonso 的 js 工作。
I apparently cant simply add a comment because I'm new to the site, but Garry's answer is correct and I've independently verified it so I wanted to add this.
CKEDITOR.instances[xxx].on('change', function() {alert('value changed!!')});
This works for CKEditor 4+, and captures all change events, including undo and redo, rendering Alfonso's js work unnecessary for new users.
不知道“change”事件,但当我使用 CKeditor 版本 3.6 时,“blur”事件对我来说非常适合
Don't know about the 'change' event but 'blur' event works perfectly for me when I am using CKeditor version 3.6
对于后来的 Google 员工,我注意到,如果您使用它创建 CKEditor,
它可以工作,但在表单提交时不会更新
mytextarea
值,因此它会发送旧值,但
如果您在表单提交时这样做,
它会发送新值
For later Googlers, I noticed that if you create CKEditor using this
it works but on form submit doesn't update
mytextarea
value so it sends old valuebut
if you do like this
on form submit it sends new value