1

首先,我搜索了该站点并发现了以下内容:

如何使用下拉选择填写文本字段

但它不适合我的需要,因为我正在生成很长的模板,并且上述方法太耗时且不切实际,因为这意味着用我的模板填充每个标签的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>使用脚本中的相应选择来填充。谢谢!

4

3 回答 3

3

好的,您在代码中有很多问题,主要是在选项中指定 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;   
 } 
}
于 2011-09-02T16:48:22.483 回答
2

onchange您已将事件绑定在option而不是select.

于 2011-09-02T16:28:45.650 回答
1

如果要在多行上有一个字符串,则必须将其连接起来:

/*option 1*/                 
" This is template 1 that  will appear in a \n"+
"textarea keeping \n"+
"its formatting \n"+
"as \n"+  
"is. \n",

\n在这里只是为了在您的 .

如果您想更改文本区域的内容,请使用属性.value而不是.innerHTML

于 2011-09-02T16:28:03.200 回答