【发布时间】:2011-09-02 16:06:32
【问题描述】:
首先,我搜索了该站点并找到了这个:
How to fill in a text field with drop down selection
但它不适合我的需要,因为我正在生成非常长的模板,并且上述方法太耗时且不切实际,因为这意味着用我的模板填充每个 <option> 标记的 value 属性.
代码如下:
<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-->
最糟糕的是,当我测试它时,我什至没有收到脚本错误,它根本不起作用,所以我不知道我哪里出错了。我已经使用<input type="text"> 标签完成了一个类似的页面,它们运行良好,但无论我尝试什么,我似乎都无法让它与<textarea> 一起使用。
任何帮助将不胜感激!提前致谢!
于 2011 年 9 月 2 日星期五下午 1: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 会解决这个错误吗?另外,我把它排除在脚本之外是因为我不知道该怎么做,而是在它说的<textarea> 中
Templates will auto-populate
here depending on the
selection made from the
[CSTemplates]
drop-down list.
当用户从下拉列表中选择一个选项时,我需要删除,并且<textarea> 需要从脚本中填充相应的选项。谢谢!
【问题讨论】:
标签: javascript drop-down-menu textarea onchange selectedindex