简体   繁体   中英

Drop down list using javascript array

Good day!

How can i make the first value in my array eg 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.

在initAll函数的末尾放:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM