动态复制表单文本区域
Dynamically replicating a form textarea
我是Javascript新手。我正在尝试创建一个用来写评论的页面。我在某一点上卡住了。
应该有一个按钮来添加一个部分,复制整个部分的div,以允许用户编写另一个部分。
下面是我的代码。我正在使用CKeditor插件,允许最终用户按照自己的意愿格式化他们的文本。
当前的代码,在创建一个新节时,不允许用户写入已创建的文本区域。请告诉我哪里搞错了。
<?php
include 'settings.php';
if (!isset($dbc)){
$dbc = new mysqli(DB_HOST , DB_USER , DB_PASSWORD , DB_NAME);
}
if ($dbc -> connect_error){
die ("Cannot connect to the database");
}
?>
<html>
<head>
<title>Write a new Review.</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<form id = "new_review" action = "form.php" method = "post">
<div id = "header">
<h2> Header Section. </h2>
Author : <input type = "text" id = "author"> <br>
Title: <input type = "text" id = "title"> <br>
Tagline: <input type = "text" id = "tagline" > <br>
Score: <input type = "text" id = "score" > <br>
Pros: <textarea class = "ckeditor" id = "pros">
Please enter the pro's of the product here.
</textarea>
Cons: <textarea class = "ckeditor" id = "cons">
Please enter the cons of the product here.
</textarea>
Verdict:<textarea class = "ckeditor" id = "verdict">
Enter your vedict here.
</textarea>
</div>
<div id = "sections">
<h2> Sections. </h2>
<input type = "button" id="button" onclick="duplicate()">Add a section</button>
<div class = "section_base" id = "section">
Section Icon: <input type="file" id="icon" accept="image/*"> <br>
Section Title: <input type = "text" id = "section_title" > <br>
Section Text: <textarea class = "ckeditor" id = "section_text">
Enter you text here.
</textarea>
Section Score: <input type = "text" id = "section_score">
</div>
</div>
<div id = "conclusion">
<h2> Conclusion: </h2>
<textarea class = "ckeditor" id = "conclusions">
Enter your conclusion here.
</textarea>
</div>
<input type = "submit" value="submit">
</form>
<script type="text/javascript">
var i = 0;
var original = document.getElementById('section');
function duplicate() {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "section" + ++i;
// or clone.id = ""; if the divs don't need an ID
original.parentNode.appendChild(clone);
}
</script>
</body>
</html>
下面是我所做的信息的链接。
http://ckeditor.com/ckeditor_4.3_beta/samples/replacebyclass.html我如何复制一个div onclick与javascript?
试试这样写
<script type="text/javascript">
var i = 1;
function duplicate() {
var clone = '<div class = "section_base" id = "section">Section Icon: <input type="file" id="icon" accept="image/*"> <br> Section Title: <input type = "text" id = "section_title" > <br> Section Text: <textarea id = "section_text'+i+'"> Enter you text here. </textarea>Section Score: <input type = "text" id = "section_score"> </div>';
var div = document.getElementById('sections');
var newdiv = document.createElement('div');
newdiv.innerHTML = clone;
div.appendChild(newdiv);
CKEDITOR.replace('section_text'+i);
i++;
}
</script>
似乎CKEditor在绑定动态添加元素的控件时遇到了一些问题。你可以参考这个问题,它包含了面临类似问题的人们的讨论和他们的解决方案。
CKEDITOR inline on dynamic created element (dropable/sorable)
还找到了这个jsfiddle演示,它绑定了CKEditor inline
CKEDITOR.inline( el.get( 0 ) );
这个家伙还写了一个很好的教程,关于如何在动态创建的元素上添加内联ckeditor
相关文章:
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- HTML文本区域文本到XML
- 阻止在文本区域/文本框中输入数字
- 使用 javascript 单击图片后更改<文本区域>文本
- 使用 javascript 检测文本区域/文本输入中的 HTML 标记,但它应该支持常见的>和<
- 在按 Tab 键时选择文本区域文本
- 切换文本区域文本,但保留对文本的更改
- 清除文本区域/文本框内容的按钮
- 加载页面时,聚焦并全选文本区域文本
- 如何使可编辑文本区域文本不可选择
- 我的文本区域文本是't用javascript将我想要的样式设置为CSS
- 使用jQuery将文本区域文本安全地存储在隐藏字段中,然后稍后在PHP中使用该文本
- 删除文本区域文本提交在新的选项卡
- 如何在添加新文本时保持文本区域文本静态
- 使用 jQuery 将文本区域文本设置为格式正确的 HTML 代码
- 为什么文本添加到区域文本是通过页面刷新,而不是直接刷新
- 使用 jquery 修改文本区域文本
- 如何使用Javascript在文本区域(文本框)中单击(光标闪烁)而不使用鼠标单击