开发者

how to make a javascript array be the choices in an <option> tag

title is self explanatory i think..

is there anyway to make a list of about 300 items in an arra开发者_JAVA百科y be the choices in

<option>ARRAY DATA</option></select>


Try this:

var s = document.getElementById('id_of_select_tag');
var ar = [1,2,3];
for(var i=0; i<ar.length; i++) {
  var option = document.createElement('option');
  option.text = ar[i];
  option.value = ar[i];
  s.options[s.options.length] = option;
}

http://jsfiddle.net/erick/9Dj3j/3


Does the JavaScript array really have to be generated from the choices in the <select>? Particularly, can't it be the other way around?

If you can get away with generating the contents of the list from the array, all you'll need to do is iterate over the array and output the list elements. I'm pretty sure you could even do that without even touching document.write() and its kin, but JavaScript DOM manipulation is far from what I know best.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜