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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5263839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:24:15  来源:igfitidea点击:

How to fill in a text field with drop down selection

javascripthtmldrop-down-menu

提问by Technupe

I have a drop down with different strings, I want a text box to be filled in with the string of the drop down selected.

我有一个包含不同字符串的下拉列表,我想要一个文本框填充所选下拉列表的字符串。

I am writing this in html

我正在用 html 写这个

<td align="right">
        <select name="xyz" size="1">
          <option value=""></option>
          <?php foreach($comments as $comment): ?>
            <?if($comment->x!= 0):?>
              <option value="<?=$comment->x;?>"<?=($comment->x == $postData["xyz"] ? 'selected="selected"':"")?>>
                   <?=$comment->y;?>
              </option>
            <?endif;?>
          <?php endforeach; ?>
        </select>
        <textarea name="xz" cols="36" rows="3"><?=$postData["xz"];?></textarea>
</td>

回答by u476945

Something like this jsfiddle demo?

像这个jsfiddle 演示

HTML:

HTML:

<textarea id="mytext"></textarea>

<select id="dropdown">
    <option value="">None</option>
    <option value="text1">text1</option>
    <option value="text2">text2</option>
    <option value="text3">text3</option>
    <option value="text4">text4</option>
</select>

JavaScript:

JavaScript:

<script type="text/javascript">
    var mytextbox = document.getElementById('mytext');
    var mydropdown = document.getElementById('dropdown');

    mydropdown.onchange = function(){
          mytextbox.value = mytextbox.value  + this.value; //to appened
         //mytextbox.innerHTML = this.value;
    }
</script>