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 日 (金) 01:17:34 PM

上記の説明を明確にするために、「これをテストしてもスクリプト エラーは発生しません。まったく機能しません」と述べました。

つまり、テンプレートをすべて 1 行に残した場合、つまり

/*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. ",

その後、「Unterminated string constant」エラーが発生します。を使用して\nこのエラーを解決しますか? また、どうすればいいのかわからないため、スクリプトから除外しましたが<textarea>、それが言う場所で

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

[CSTemplates] 

drop-down list.

ユーザーがドロップダウンリストから選択したときに消去し<textarea>、スクリプトから対応する選択を入力する必要があります。ありがとう!

4

3 に答える 3

3

さて、あなたはコードに多くの問題を抱えていましたが、主なものは選択の代わりにオプションでonchangeを指定していました。私が解決した問題もほとんどありません。

ここにマークアップがあります

<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イベントをバインドしました。optionselect

于 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 に答える