开发者

Drop down list using javascript array

开发者 https://www.devze.com 2023-03-08 05:42 出处:网络
Good day! How can i make the first value in my array e.g currency[0] be the default value of my drop down list?

Good day!

How can i make the first value in my array e.g currency[0] be the default value of my drop down list?

My code is as follows:

function addOption(selectbox,text,value )
    {
        var optn = document.createElement("OPTION");
        optn.text = text;
        optn.value = value;
        selectbox.options.add(optn);
    }

    function initAll(){
        var currency = new Array("Peso","Dollar","Euro","Yen","CAD");
        var rate = new Array("0.02319", "1", "1.416", "0.012241", "1.027");
        for (var i=0; i < currency.length;++i){
            addOption(document.drop_list.Month_list, currency[i开发者_如何学编程],rate[i]);
        }
    }

I want the default value be the first array...

<FORM name="drop_list">
   <select name="Month_list">: 
       <Option value="" >-----</option>  //what could i insert here?
   </select><br>
</form>

You're help would be highly appreciated.


In the end of initAll function put :

document.drop_list.Month_list.selectedIndex=1;


Use Adelave's solution if you decide to have the first option as "-----". You may altogether remove the hard-coded option "-----" and select drop-down would be auto-selected with the first option in the drop-down box.

0

精彩评论

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

关注公众号