根据选择添加值

时间:2012-04-30 16:42:33

标签: javascript html

如何根据上面下拉框中的用户选择将“变量值”添加到我的文本区域。

这是我到目前为止的代码:

<html>
<head>

<body>

<select id="dropdown">
    <option value="">None</option>
    <option value="textArray1">text1</option>
    <option value="textArray2">text2</option>
    <option value="textArray3">text3</option>
    <option value="textArray4">text4</option>
</select>

<br /><br />

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

<script type="text/javascript">

var textArray1 = 'this is going to be a long sting of text for text 1 value';
var textArray2 = 'this is going to be a long sting of text for text 2 value';
var textArray3 = 'this is going to be a long sting of text for text 3 value';
var textArray4 = 'this is going to be a long sting of text for text 4 value';

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>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试在onchange函数中进行此更改:

mytextbox.value = mytextbox.value  + window[this.value]; //to appened

你也可以在this.value上使用eval(),但是eval()和红头步子一样受欢迎。有关差异的详细信息,请参阅this问题。

<强> jsFiddle example