/!\ Resolve /!\创建一个在JS中选择的选项

时间:2014-02-28 09:12:02

标签: javascript select selected

[解决] 好吧,我发现问题......我是如此愚蠢-_- 对不起(在创建选择之后,我调用了一个将selectedIndex设置为0的函数...)。

我尝试用JS创建一个选择(这里一切都好)但我无法选择特定的选项!我尝试了所有解决方案,但没有任何效果......

这是我的代码: 响应是一张包含我所有选择的表格。

function creatSelect(reponse) {

    var name_div = document.getElementById('ChooseModele');
    var select = document.createElement("select");
    select.setAttribute("name", "ListModele");
    select.setAttribute("id", "ListModele");
    select.style.width = "300px";
    select.setAttribute("onchange", 'updateModele(this.options[this.selectedIndex].value)');
    var option = document.createElement("option");
    option.setAttribute("value", "");
    option.innerHTML = "";
    select.appendChild(option);
    for (var nbmessage=0;nbmessage<reponse.length;nbmessage++)
        {
        option = document.createElement("option");
        option.setAttribute("value", reponse[nbmessage].name);
        option.innerHTML = reponse[nbmessage].name;
        if(reponse[nbmessage].name=='**modele i want selected**')
            **option.setAttribute("selected","selected");**
        select.appendChild(option);
        }
    name_div.appendChild(select);

}

3 个答案:

答案 0 :(得分:0)

您需要使用element.options[index].selected属性。

所以,试试这个:(未经测试)

function creatSelect(reponse) {

    var name_div = document.getElementById('ChooseModele');
    var select = document.createElement("select");
    select.setAttribute("name", "ListModele");
    select.setAttribute("id", "ListModele");
    select.style.width = "300px";
    select.setAttribute("onchange", 'updateModele(this.options[this.selectedIndex].value)');
    var option = document.createElement("option");
    option.setAttribute("value", "");
    option.innerHTML = "";
    select.appendChild(option);
    var selectedIndex = 0;
    for (var nbmessage=0;nbmessage<reponse.length;nbmessage++)
        {
        option = document.createElement("option");
        option.setAttribute("value", reponse[nbmessage].name);
        option.innerHTML = reponse[nbmessage].name;
        if(reponse[nbmessage].name=='**modele i want selected**')
            selectedIndex = nbMessage; // save the selected option index
        select.appendChild(option);
        }
    select.options[selectedIndex].selected = true; // here, you set the default selected option
    name_div.appendChild(select);

}

答案 1 :(得分:0)

您的代码工作正常(我使用最新的Chrome测试了这个小提琴:http://jsfiddle.net/enJ5F/1/

只是为了好玩,这是一个使用jQuery的解决方案:

function createSelect(response) {
    var $select = $( "<select>" ).appendTo($( "#ChooseModele" ));
    $.each(response, function(key, value) {
        var selected = value.name == 'modele i want selected';
        $( "<option>", { value: value.name, selected: selected } )
            .text( value.name )
            .appendTo( $select );
    });
}

显示其运作方式的小提琴:http://jsfiddle.net/ny6m5/

答案 2 :(得分:0)

您可以保留option's index,然后按照 option 选择index

var selectedIndex = 0;
for (var nbmessage=0;nbmessage<reponse.length;nbmessage++)
{
    if(reponse[nbmessage].name=='**modele i want selected**')
        selectedIndex = nbmessage;
}

select.selectedIndex = selectedIndex;