当
我在 ajax 场景中使用 ckeditor 和 jQuery 适配器。使用 jQuery 将 ckeditor 绑定到 textarea 元素。
应用程序:
我正在为客户设计一个电子学习应用程序。每门课程都有许多步骤,这些步骤取自数据库表。
该应用程序的 UI 只是一个带有“上一个”和“下一个”按钮的表单,用于在课程中导航。
问题在于管理工具,导师可以在其中创建和更新每门课程中的步骤。
同样,管理工具只是一个带有上一个和下一个按钮的表单。但是,有一个名为 StepText 的字段,我想要 html 而不是纯文本。因此需要富文本编辑器。
从一个步骤到另一个步骤的滚动是通过 Ajax 调用进行的,这些调用会发送到控制器,获取下一个(或上一个)步骤的数据,并将其注入到页面中。 ajax 调用返回从部分视图创建的 html。
问题:
文本区域位于此部分视图中。第一次加载页面时,CKEditor 会正确绑定,但对于任何后续的 ajax 响应则不会。
结果是 ckEditor 并没有展现出它所有的优点,我只是得到了毫无吸引力的文本区域。
有什么想法为什么会发生这种情况吗?
设置(显示供参考)(已编辑):
使用以下脚本:
<script type="text/javascript" src="@Url.Content("~/ckeditor/ckeditor.js")"></script>
<script type="text/javascript" src="@Url.Content("~/ckeditor/adapters/jquery.js")"></script>
这允许我将 ckEditor 与 jQuery 一起使用,如下所示:
$(document).ready(function () {
$('#StepText').ckeditor(function () { }, { toolbar: 'Basic' });
});
文本区域位于部分 .cshtml 文件中,位于充当 ajax 调用目标的 div 中:
已编辑!显示如何进行 ajax 调用!!
<div id="ajaxEditor">
@using (Ajax.BeginForm(MVC.Admin.StepEditor.Edit(), new AjaxOptions { UpdateTargetId = "ajaxEditor", HttpMethod = "POST" },
new { @name = "EditStepForm", @id = "EditStepForm" }))
{
<div class="editor-label">
@Html.LabelFor(model => model.StepText)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.StepText, 20, 68, null)
@Html.ValidationMessageFor(model => model.StepText)
</div>
}
</div>
回复 Chris Marisic:
我尝试在 ajax 调用的 OnSuccess 中重新绑定 ckeditor,但是在 Chrome 中调试 jQuery 时,出现以下错误
Uncaught [CKEDITOR.editor] The instance "myTextArea" already exists.
:会期望:控件名称仍然相同并且没有全页刷新。
I am using ckeditor with the jQuery adapter in an ajax scenario. The ckeditor is bound to a textarea element using jQuery.
The App:
I am designing an eLearning app for a client. Each course has many steps, drawn from a database table.
The UI of the app is just a form with Previous and Next buttons to navigate through the course.
The issue is with the admin facility, where Tutors can create and update the steps in each course.
Again, the admin facility is just a form with previous and next buttons. However, there is a field called StepText that I want to have html rather than plain text. Hence the requirement for a Rich Text Editor.
The scrolling from step to step is down with Ajax calls, that go off to the controller, get the data for the next (or previous) step, and inject it into the page. The ajax calls return html, created from a partial view.
The Problem:
The textarea is in this partial view. The CKEditor binds correctly the first time the page is loaded, but not for any subsequent ajax responses.
The result is that the ckEditor does not appear with all its goodness and I just get the unappealing nakedness of the textarea.
Any ideas why this is happening?
SETUP (Shown for Refrence) (Edited):
The following scripts are used:
<script type="text/javascript" src="@Url.Content("~/ckeditor/ckeditor.js")"></script>
<script type="text/javascript" src="@Url.Content("~/ckeditor/adapters/jquery.js")"></script>
This allows me to use ckEditor with jQuery as follows:
$(document).ready(function () {
$('#StepText').ckeditor(function () { }, { toolbar: 'Basic' });
});
The text area is in a partial .cshtml file, within a div that acts as the target for the ajax call:
Edited!! to show how the ajax call is made !!
<div id="ajaxEditor">
@using (Ajax.BeginForm(MVC.Admin.StepEditor.Edit(), new AjaxOptions { UpdateTargetId = "ajaxEditor", HttpMethod = "POST" },
new { @name = "EditStepForm", @id = "EditStepForm" }))
{
<div class="editor-label">
@Html.LabelFor(model => model.StepText)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.StepText, 20, 68, null)
@Html.ValidationMessageFor(model => model.StepText)
</div>
}
</div>
Response to Chris Marisic:
I tried rebinding the ckeditor in the OnSuccess of the ajax call, but when debugging the jQuery in Chrome, I get the following error:
Uncaught [CKEDITOR.editor] The instance "myTextArea" already exists.
Which is what you would expect: the control name is still the same and there has not been a full page refresh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
打电话给 Chris Marisic:
如果 Chris Marisic 想要将他的评论升级为答案,我会给他饼干。
我已经解决如下:
Ajax Form:
Javascript:
由于返回的错误(请参阅对有问题的 Chris Marisic 的回复)表明 CKEDITOR 实例已经存在,我需要摆脱它。所以:
BindCKEditor() 就按照它所说的去做。它配置编辑器并应用它。
现在,当我用 ajax 响应覆盖文本区域时,我需要删除该实例并重新创建它。所以:
请注意,我在 Ajax 表单的 OnComplete 中使用 ReBindCKEditor()。我使用 oncomplete 而不是 onsuccess,因为我希望它重新绑定,无论 ajax 调用是否成功。
成功!:
我极力推荐 ckeditor。我认为这将是一个很难解决的噩梦,但是一旦我坐下来把这些想法放在一边,我发现有非常简洁的文档和很好的用户论坛。
我查看了很多其他富文本编辑器,但没有一个是那么好或得到很好的支持。雅虎的看起来不错,但是是一场噩梦,而且文档很差,除非你是 JavaScript 专家,而我不是。
我几乎选择了 lwrte,它看起来不错,但背后没有支持。
我看了看其他未命名的。
但ckEditor确实给我留下了深刻的印象。我现在可以将其推出到我想要的任何地方,而无需担心或恐惧。
注意:
我不为 CKEditor 工作,也与他们没有任何联系。这是真正的热情。是的,这样的事情确实存在。
Calling Chris Marisic:
If Chris Marisic wants to upgrade his comment to an answer, I will give him the biscuit.
I have solved as follows:
Ajax Form:
Javascript:
As the error coming back (see response to Chris Marisic in question) states that the CKEDITOR instance already exists, I need to get rid of it. So:
BindCKEditor() just does what it says on the tin. It configures the editor and applies it.
Now, when I overwrite the text area with the ajax response, I need to delete that instance and recreate it. So:
Note that I use ReBindCKEditor() in the OnComplete for the Ajax Form. I use oncomplete and not onsuccess, because I want it to rebind regardless of the success or otherwise of the ajax call.
SUCCESS!:
I can't recommend ckeditor highly enough. I thought this was going to be a nightmare to solve, but once I sat down and put such thoughts aside, I found that there was very concise documentation and good user forums.
I looked at a lot of other Rich Text Editors, and none were as good or as well supported. Yahoo's one looked good, but was a nightmare and documentation poor unless you are a javascript expert which I ain't.
I nearly went with lwrte, it looked good, but no support behind it.
I looked at other that shall remain unnamed.
But ckEditor really has impressed me. I can now roll it out where I want to with no fear or trepidation.
Note:
I am not working for CKEditor nor have any connection with them. This is genuine enthusiasm. Yes, such a thing does exist.
这对于我使用 jquery ckeditor 适配器最有效。我只是在用 ckeditor textarea 加载一些 ajax 时调用它。
}
this worked best for me using the jquery ckeditor adapter. I just call this when I load up some ajax with a ckeditor textarea.
}
我也遇到了这个问题,但是 senario 有点不同,我在 Ajax.BeginForm 中使用了 ckeditor,但是我的表单(部分视图)出现在 jquery ui 对话框中,第一个答案给了我一个解决这个问题的好主意。当对话框出现时,我重新绑定了 ckeditor!
$('input[type=submit]').bind('click', function () {
$('.ckeditor').ckeditorGet().updateElement();
});
CKEDITOR.editorConfig =
开始new { @class = "ckeditor" })
到 TextAreaFor(partial view) 并在父页面中添加此行delete CKEDITOR.instances['FieldName'];
在父页面中我调用对话框:
在我使用 Ajax.BeginForm 的部分视图中:
如您所见,我没有使用 OnCompelete 或 OnBegin。希望它能帮助别人!
I had this problem too, but the senario was a bit different, I used ckeditor in an Ajax.BeginForm but my form(partial view) is coming up in a jquery ui dialog, the first answer gave me a great idea to fix that. I did rebinding for ckeditor when dialog is coming up!
$('input[type=submit]').bind('click', function () {
$('.ckeditor').ckeditorGet().updateElement();
});
CKEDITOR.editorConfig =
new { @class = "ckeditor" })
to TextAreaFor(partial view) and adding this line in parent pagedelete CKEDITOR.instances['FieldName'];
in the parent page i call the dialog:
in my partial view with Ajax.BeginForm:
as you see I did NOT use OnCompelete or OnBegin. hope it helps somebody!