如何从我的javascript函数的值填充HTML组合框. [英] How to populate a HTML combobox from the values of my javascript function.

查看:67
本文介绍了如何从我的javascript函数的值填充HTML组合框.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的新工作是使用具有数组的javascript函数传递的值填充HTML组合框.我的html标签是这样的

Hi,

My new work is to populate a HTML combobox with the values passed from a javascript function having an array. My html tag is like this

<body  önLoad="firstitle()">
<form name="register">
<select id="mydropdown" name="mydropdown" ></select>
</body>


而我的javascript函数是这样的


and my javascript function is like this

<pre lang="Javascript">function firstitle()
  {
   var title=new Array("Mr.","Mrs.","Ms.");
   for(var i=0;i<title.length;i++)
   {
   var option= new Option(title[i],title[i]);
   mydropdown.options[i]=option;
   this.alert(title[i]);
   }
   }


上面的代码适用于firefox和chrome,但无法加载ie8中的下拉列表.任何人都可以提供帮助.


The above code works with firefox and chrome but fails to load the dropdown in ie8. Can anybody help.

推荐答案

如果您明确使用document.createElement,它将在跨浏览器中正常工作.

If you explicitly use document.createElement it will work cross browser.

function firsttitle() {
    var title = new Array("Mr.", "Mrs.", "Ms."); // Possible Values
    var sel = document.getElementById('mydropdown') // find the drop down
    for (var i in title) { // loop through all elements
        var opt = document.createElement("option"); // Create the new element
        opt.value = title[i]; // set the value
        opt.text = title[i]; // set the text

        sel.appendChild(opt); // add it to the select
    }
}


这篇关于如何从我的javascript函数的值填充HTML组合框.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆