如何在Javascript中添加选项时设置下拉列表的值?

时间:2013-01-29 06:52:50

标签: javascript jquery

我使用下面的代码添加下拉列表选项

             //HTML CODE
            select(name='folist',  id='folist').
            //JS Code
             for(var i=0;i<data.foNameArray.length;i++){
            var combo = document.getElementById("folist");

            option = document.createElement("option");
            option.text = data.foNameArray[i];
            option.value =data.foIdArray[i];
             try {
                combo.add(option, null); //Standard 
            }catch(error) {
                combo.add(option); // IE only
            }
            }

它完美有效。现在我怀疑如何在添加like selected =“selected”时选择值。

2 个答案:

答案 0 :(得分:2)

您可以像这样设置selected property选项,

option.selected = true;

答案 1 :(得分:2)

您可以使用setAttribute来执行此操作

option = document.createElement("option");
option.setAttribute("selected","selected");