javascript - 根据下拉选择将文本加载到文本区域

标签 javascript drop-down-menu textarea onchange selectedindex

首先,我搜索了网站并找到了这个:

How to fill in a text field with drop down selection

但它不符合我的需要,因为我生成的模板很长,而且上述方法太耗时且不切实际,因为这意味着要填写 value每个 <option> 的属性用我的模板标记。

代码如下:

<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>使用脚本中的相应选择进行填充。谢谢!

最佳答案

好吧,你在代码中遇到了很多问题,主要问题是在选项中指定 onchange 而不是选择,我也解决了一些小问题,这里是一个工作示例 http://jsfiddle.net/gKsdK/1/

这是标记

<h1>Note Generator</h1>

<div class="left">
CSTemplates
<p>
<select class="c10" onchange="showCSTemplates(this);"> 
<option selected="selected" value="" id="Templates">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-->

这是JS

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;   
 } 
}

关于javascript - 根据下拉选择将文本加载到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286212/

相关文章:

php - 我无法在 php mysql 中插入所选下拉菜单的值

php - Paypal 立即购买按钮 - 下拉数量

java - TextArea 适合 GridPane

javascript - 使用 'enter' key 发送推文 (Greasemonkey)

jquery - 如何使用 css 和 jquery 给文本区域的滚动选项提供一定的高度

javascript - Web 应用程序国际化,是服务器端还是客户端?

javascript - 通过远程按钮激活 JQuery UI 日期选择器

html - 当下拉列表更改其内容时,带下拉列表的表不会在 IE 7 中调整大小

javascript - 我没有得到 Nodejs npm 包 https 的 https.get() 与 https.request() 方法之间的区别

javascript - 简单的第一个 child js?