根据下拉选择将文本加载到文本区域
首先,我搜索了该网站并发现了这个:
但它不适合我的需求,因为我正在生成很长的模板,并且上述方法太耗时且不切实际,因为这意味着填写值
我的模板的每个 标记的属性。
所以这是代码:
<script type="text/javascript">
//<![CDATA[
function showCSTemplates(sel){
locations =[ "", /*this remains blank for first selection in drop-down list*/
/*option 1*/
" This is template 1 that will appear in a
textarea keeping
its formatting
as
is. ",
/*option 2*/
" This is template 2 that
will appear in a
textarea keeping its
formatting as is.
Credentials:
Contact Info: ",
/*option 3*/
" This is template 3 that will appear in a
textarea keeping its formatting as is.
Donec tortor lorem,
ornare vitae commodo nec,
sagittis et nunc.
Maecenas sagittis quam ",
/*option 4*/
"etc",
/*option 5*/
"etc...", ];
srcLocation = locations [sel.selectedIndex];
if (srcLocation != undefined && srcLocation != "") {
document.getElementById('CSTemplates').innerHTML = srcLocation;
}
} //]]>
</script>
这是标记:
<h1>Note Generator</h1>
<div class="left">
CSTemplates
<p>
<select class="c10">
<option selected="selected" value="" id="Templates" onchange="showCSTemplates(this);">Please select a template...</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</p>
<p>
<textarea cols="30" rows="20" readonly="readonly" id="CSTemplates">
Templates will auto-populate
here depending on the
selection made from the
[CSTemplates]
drop-down list.
</textarea>
</p>
</div><!--left ends here-->
最糟糕的部分是,当我测试这个时,我什至没有收到脚本错误,它根本不起作用,所以我不知道我哪里出了问题。我使用 标签完成了类似的页面,它们工作正常,但我似乎无法让它与
任何帮助将非常感激!提前致谢!
编辑于 2011 年 9 月 2 日星期五下午 01:17:34
为了澄清上面我所说的“当我测试这个时,我什至没有收到脚本错误,它只是没有”根本不起作用,”
我的意思是,如果我将模板全部保留在一行上,
/*option 1*/
" This is template 1 that will appear in a textarea keeping its formatting as is. ",
即不会出现错误。但是,如果我输入换行符以进行如上所述的格式化:
/*option 1*/
" This is template 1 that will appear in a
textarea keeping
its formatting
as
is. ",
那么我会收到“未终止的字符串常量”错误。使用 \n
可以解决此错误吗?另外,我将其排除在脚本之外,因为我不知道如何处理它,但是在
Templates will auto-populate
here depending on the
selection made from the
[CSTemplates]
drop-down list.
当用户从dropdownlist,并让
To start, I have searched the site and found this:
How to fill in a text field with drop down selection
but it did not suit my needs, as I'm generating very long templates and the above method would be too time consuming and impractical as it would mean filling in the value
attribute of each <option>
tag with my templates.
So here's the code:
<script type="text/javascript">
//<![CDATA[
function showCSTemplates(sel){
locations =[ "", /*this remains blank for first selection in drop-down list*/
/*option 1*/
" This is template 1 that will appear in a
textarea keeping
its formatting
as
is. ",
/*option 2*/
" This is template 2 that
will appear in a
textarea keeping its
formatting as is.
Credentials:
Contact Info: ",
/*option 3*/
" This is template 3 that will appear in a
textarea keeping its formatting as is.
Donec tortor lorem,
ornare vitae commodo nec,
sagittis et nunc.
Maecenas sagittis quam ",
/*option 4*/
"etc",
/*option 5*/
"etc...", ];
srcLocation = locations [sel.selectedIndex];
if (srcLocation != undefined && srcLocation != "") {
document.getElementById('CSTemplates').innerHTML = srcLocation;
}
} //]]>
</script>
and here's the markup:
<h1>Note Generator</h1>
<div class="left">
CSTemplates
<p>
<select class="c10">
<option selected="selected" value="" id="Templates" onchange="showCSTemplates(this);">Please select a template...</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</p>
<p>
<textarea cols="30" rows="20" readonly="readonly" id="CSTemplates">
Templates will auto-populate
here depending on the
selection made from the
[CSTemplates]
drop-down list.
</textarea>
</p>
</div><!--left ends here-->
The worst part is, I'm not even getting a script error when I test this, it just doesn't work at all, so I don't know where I went wrong here. I've done a similar page using <input type="text">
tags and they worked fine, but I can't seem to get it to work with the <textarea>
at all no matter what I try.
Any help will be very much appreciated! Thanks in advance!
Edited on Fri September 2, 2011 at 01:17:34 PM
To clarify the above where I said "I'm not even getting a script error when I test this, it just doesn't work at all,"
what I mean was, if I leave the templates all on one line i.e.
/*option 1*/
" This is template 1 that will appear in a textarea keeping its formatting as is. ",
then I don't get an error. If I enter line breaks however for formatting like above:
/*option 1*/
" This is template 1 that will appear in a
textarea keeping
its formatting
as
is. ",
then I get an "Unterminated string constant" error. Would using \n
solve this error? Also, I left it out of the script because I don't know how to go about it, but in the <textarea>
where it says
Templates will auto-populate
here depending on the
selection made from the
[CSTemplates]
drop-down list.
I need to erase when the user picks a selection from the dropdownlist, and for the <textarea>
to populate with the corresponding selection from the script. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您在代码中遇到了很多问题,主要问题是在选项中指定 onchange 而不是选择,我也解决了一些小问题,这里是一个工作示例 http://jsfiddle.net/gKsdK/1/
这是标记
这是 JS
Okay you had many problems in code the major one was specifying onchange in option instead of selects, there also little problem which I solved and here is a working example http://jsfiddle.net/gKsdK/1/
Here is the markup
Here is the JS
您已将
onchange
事件绑定在option
上,而不是select
上。You have bound the
onchange
event onoption
instead ofselect
.如果你想在多行上有一个字符串,你必须将它连接起来:
\n 在这里只是为了在你的 .
如果您想更改文本区域的内容,请使用属性 .value 而不是 .innerHTML !
If you want to have a string on several lines, you have to concatenate it:
The \n are here only to create the breaklines into your .
And if you want to change the contents of a textarea, use the property .value and not .innerHTML !