为什么我的动态 CMS 表单不是每次都加载?
我正在使用 CKEditor 创建 CMS 的一部分,供用户输入内容。我的 CMS 是顶部的一个栏/菜单,其中包含网站的各个部分,供用户创建、更新或删除条目。
当用户选择一个选项时,我使用 jquery AJAX $.post 将表单项的请求发送到 php。该函数返回代码,我使用 $('#loadCMS').html(data) 创建表单而无需重新加载页面。这项工作很棒,调试总是显示返回的正确代码。
但是,CKEditor 仅在第一次选择项目时加载。它可能会再次加载,但很少见。
CKEditor 是位于头部的 javascript,并用
<head>
...
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
...
</head
动态加载的编辑器替换指定的文本区域以调用编辑器
<textarea name="editor1"></textarea>
<script type="text/javascript">
CKEDITOR.replace( "editor1",
{
toolbar :
[
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-', 'SpellChecker'],
['Undo','Redo','-','RemoveFormat'],
['Bold','Italic','Underline'],
['Subscript','Superscript'],
['NumberedList','BulletedList'],
['Link','Unlink'],
['Image','Flash','HorizontalRule','SpecialChar','Format'],
['Maximize', 'ShowBlocks','-','About']
],
width : '1000',
height : '300',
filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?Type=Images',
filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?Type=Flash'
});
</script>
jquery
$('#portfolioCreate').click(function()
{
var detailsList = new Array('title','medium','original');
$.post('cms.php',{detailsList: detailsList,images:"imageOn",subimgNum:0,content1:"Comments"},
function(data)
{
$('#loadCMS').html(data);
$('#debug').val(data);
});
});
每次都会动态创建表单。然而,CKEditor 替换的文本区域并不总是替换,它只是空白,甚至连文本区域框也不显示。第一次进行选择时,它会起作用。如果用户选择创建新的博客条目,则文本区域将被替换,如果他们随后选择更新个人简介,则即使他们返回创建新的博客条目,也不会替换任何文本区域。
-----------------解决方案-------------------------
动态 php
$key = md5(time().rand())
<textarea name="'.$key.'"></textarea>
<script type="text/javascript">
CKEDITOR.replace("'.$key'",
{
.....
});
<input type="hidden" value="'.$key.'" name="content1Key" />
</script>
php 从表单中提取
$content1Key = $_POST['content1Key'];
$content1 = $_Post[$content1Key];
I'm using CKEditor to created the portion of my CMS for the user to input content. My CMS is a bar/menu at the top with the sections of the site for the user to create, update, or delete an entry.
When the user selects an option I send the request for the form items to php using jquery AJAX $.post. The function returns the code and I use $('#loadCMS').html(data) to create the form without reloading the page. This work great and debugging always shows the correct code returned.
However, CKEditor only loads the first time an item is selected. It may load again but it's rare.
CKEditor is javascript that sits in the head and replaces specified textareas with the editor
<head>
...
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
...
</head
what is loaded dynamically to call the editor
<textarea name="editor1"></textarea>
<script type="text/javascript">
CKEDITOR.replace( "editor1",
{
toolbar :
[
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-', 'SpellChecker'],
['Undo','Redo','-','RemoveFormat'],
['Bold','Italic','Underline'],
['Subscript','Superscript'],
['NumberedList','BulletedList'],
['Link','Unlink'],
['Image','Flash','HorizontalRule','SpecialChar','Format'],
['Maximize', 'ShowBlocks','-','About']
],
width : '1000',
height : '300',
filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?Type=Images',
filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?Type=Flash'
});
</script>
jquery
$('#portfolioCreate').click(function()
{
var detailsList = new Array('title','medium','original');
$.post('cms.php',{detailsList: detailsList,images:"imageOn",subimgNum:0,content1:"Comments"},
function(data)
{
$('#loadCMS').html(data);
$('#debug').val(data);
});
});
The forms are created dynamically every time. However, the textareas replaced by CKEditor does not always replace, it's just blank not even the textarea box shows. The first time a selection is made it works. If the user chooses to create a new blog entry the text area is replaced, if they then choose to update bio no text area is ever replaced even if they go back to create a new blog entry.
-----------------SOLUTION-------------------------
dynamic php
$key = md5(time().rand())
<textarea name="'.$key.'"></textarea>
<script type="text/javascript">
CKEDITOR.replace("'.$key'",
{
.....
});
<input type="hidden" value="'.$key.'" name="content1Key" />
</script>
php to pull from form
$content1Key = $_POST['content1Key'];
$content1 = $_Post[$content1Key];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CKEditor 对动态创建表单非常挑剔,因为它们存储引用的方式(在一个 由文本区域名称键控的全局数组我相信)。尝试为每个文本框指定一个唯一的名称,以避免它尝试返回已被破坏/覆盖的现有引用,并且在加载每个新表单后,您需要调用
CKEDITOR.replace
。CKEditor is very picky about dynamically creating forms because of the way they store references (in one global array keyed by textarea name I believe). Try to give each textbox a unique name to avoid it trying to return an existing reference that's been destroyed/overwritten, and after loading each new form you need to call
CKEDITOR.replace
.