开发者

Javascript: Get array value when I select the option in the drop down list

I hope that someone would be able to help me to solve this simple problem.

My goal is to get the value in the array from selection drop down list.

Basically, I create an Array in Javascript and a selection drop down list in the body.

<script type="text/javascript">
    var even = new Array(2, 4, 6);
</script>
.
.
.
<select id="evenNumbers">
     <option value="1">two</option>
     <option value="2">开发者_StackOverflow中文版;four</option>
     <option value="3">six</option>
</select>

My question is how can I get the value in the Array if I select the option from the drop down list? e.g. when I select "two" from the drop down list, but I can get the value of "2" in the array in order to do some calculation.


your select box will look like this

<select >
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

if you want to populate values putting a for loop.

for getting selected option value

this.options[this.selectedIndex].value

http://awesomerails.wordpress.com/2007/12/04/get-the-value-of-a-selected-option-with-javascript/


document.getElementById('evenNumbers').onchange = function() {
  var index = this.value - 1; // array indices start at 0
  alert(even[index]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜