复选框文本到文本区域



我需要有很多句子,可以通过复选框选择,它们将立即出现在它们下面的文本区域。在文本区域可以更改句子,然后将文本区域导出为docx和pdf。我从用户dinheiro的论坛中找到了2007年的这个不错的脚本 Sitepoint.com。

每个句子都应该在自己的行上,句子之间应该换行。添加句子时一切顺利,但删除句子时,句子之间会错过换行。我在网上对原始脚本进行了一些更改

df[1][0].value+=this.name+'nn ';

原版是df[1][0].value+=this.name+' ';

并添加了一行df[1][0].value=df[1][0].value.replace(/n/g, '');

任何人都可以帮助解决这个问题,或者有人有更好的解决方案来做到这一点吗?谢谢!

window.onload = function() {
df = document.forms;
inp = df[0].getElementsByTagName('input');
for (c = 0; c < inp.length; c++) {
if (inp[c].type == 'checkbox') {
inp[c].onclick = function() {
if (this.checked == true) {
df[1][0].value += this.name + 'nn ';
} else {
df[1][0].value = df[1][0].value.replace(/n/g, '');
df[1][0].value = df[1][0].value.replace(this.name + ' ', '');
}
}
}
}
}
<form action="#">
<div>
<input type="checkbox" name="- foo" />foo<br>
<input type="checkbox" name="- blah" />blah<br>
<input type="checkbox" name="- dodah" />dodah<br>
</div>
</form>
<form action="#">
<div>
<textarea cols="20" rows="10"></textarea>
</div>
</form>

如何将复选框文本添加到文本区域,编辑文本区域上的文本,添加一些额外的文本并将其与其他字段一起发送到处理程序?

问题是这一行删除了使文本出现在同一行中的换行符

df[1][0].value=df[1][0].value.replace(/n/g, '');

因此,我只迎合了与要删除的特定单词相关的换行符:

df[1][0].value=df[1][0].value.replace(this.name+'nn','');

这是工作代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">

window.onload=function() {

df=document.forms;
inp=df[0].getElementsByTagName('input');
for(c=0;c<inp.length;c++) {
if(inp[c].type=='checkbox') {
inp[c].onclick=function() {
debugger;
if(this.checked==true){
df[1][0].value+=this.name+'nn';
}
else {
df[1][0].value=df[1][0].value.replace(this.name+'nn','');
}
}
}
}
}
</script>

</head>
<body>

<form action="#">
<div>
<input type="checkbox" name="- foo"/>foo<br>
<input type="checkbox" name="- blah"/>blah<br>
<input type="checkbox" name="- dodah"/>dodah<br>
</div>
</form>

<form action="#">
<div>
<textarea cols="20" rows="10"></textarea>
</div>
</form>

</body>
</html>

最新更新