【问题标题】:loading text into textarea based on drop down selection基于下拉选择将文本加载到文本区域
【发布时间】: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-->

最糟糕的是,当我测试它时,我什至没有收到脚本错误,它根本不起作用,所以我不知道我哪里出错了。我已经使用&lt;input type="text"&gt; 标签完成了一个类似的页面,它们运行良好,但无论我尝试什么,我似乎都无法让它与&lt;textarea&gt; 一起使用。

任何帮助将不胜感激!提前致谢!

于 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 会解决这个错误吗?另外,我把它排除在脚本之外是因为我不知道该怎么做,而是在它说的&lt;textarea&gt;

Templates will auto-populate 
here depending on the 
selection made from the 

[CSTemplates] 

drop-down list.

当用户从下拉列表中选择一个选项时,我需要删除,并且&lt;textarea&gt; 需要从脚本中填充相应的选项。谢谢!

【问题讨论】:

    标签: javascript drop-down-menu textarea onchange selectedindex


    【解决方案1】:

    好的,你在代码中有很多问题,主要是在选项中指定 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 还是很陌生。然而,即使使用了 KorHosik 的建议,我似乎仍然无法让换行符工作
    • 从头开始,忘记将 innerhtml 更改为 value。换行符现在正在工作,谢谢! =)
    • 很高兴听到这个消息:),不要感到尴尬,如果您有任何问题,请随时在这里提问。
    【解决方案2】:

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

    【讨论】:

    • 这里面有多个问题,但是这个才是杀手锏。其他问题,字符串中的换行符需要转义。变量应该用var 声明。这是它的一个 jsFiddle:jsfiddle.net/mHZgj/1
    【解决方案3】:

    如果你想在多行上有一个字符串,你必须将它连接起来:

    /*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

    【讨论】:

    • 你也可以用反斜杠转义换行符。
    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多