javascript 我如何在 Dojo form.select 选项中设置 selected

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12077916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:15:57  来源:igfitidea点击:

how do i set selected in Dojo form.select option

javascriptdojo

提问by setlio

Hi there Dojo developers, I have a drop down form.select, and it has few options, how do set an option to be selected. Say I want to have the third option displayed in the select element. I was looking at the dojo docs and I do not see setSelected() or similar.

嗨,Dojo 开发人员,我有一个下拉 form.select,它有几个选项,如何设置要选择的选项。假设我想在 select 元素中显示第三个选项。我正在查看 dojo 文档,但没有看到 setSelected() 或类似内容。

Thanks

谢谢

采纳答案by Sandeep

You need to use displayedValue property in addition to value to set the displayed option. Use something like:

除了 value 之外,您还需要使用 displayValue 属性来设置显示的选项。使用类似的东西:

selector.set("displayedValue", "the_text_of_the_option");

selector.set("displayedValue", "the_text_of_the_option");

or you can search the underlying store of your drop down by using :

或者您可以使用以下命令搜索下拉列表的基础商店:

selectorStore.fetch({query:{id: value}, onComplete: function (items) {
              dojo.forEach(items, function(item){
                  selector.set("displayedValue", "the_text_of_the_option");
                  selector.set("value", "the_value_of_the_option");
              });
}});

Hope that helps.

希望有帮助。

回答by setlio

Thank you, this is true and working. I have tested it. However i discovered my bug: I was creating the options dynamically, and when I set .selected = true as soon as I add it to the selector it changes the sated to the first one being selected. Or if I apply selector.set("displayedValue", "the_text_of_the_option");It displays visually the selected one but in fact behind the selected is still the first one does not meter if I change it with the above selector.set. So I solved it by manually creating the selected state. This way when I add it letter id stays in the desired one and changes it accordingly.

谢谢,这是真的并且有效。我已经测试过了。但是我发现了我的错误:我正在动态创建选项,当我将 .selected = true 添加到选择器时,它会将 sated 更改为第一个被选中的选项。或者,如果我应用selector.set("displayedValue", "the_text_of_the_option");它在视觉上显示选定的一个,但实际上在选定的后面仍然是第一个,如果我用上面的 selector.set 更改它,则它不会计量。所以我通过手动创建选定状态来解决它。这样,当我添加字母 id 时,它会保留在所需的字母中并相应地更改它。

Snipped here:

剪在这里:

    //populate latitude selector
    match = false;
    optionsArr = [];
    for(var i = 0; i < namesLength; i++){
        for(var j = 0, len2 = latNames.length; j < len2; j++){
            if(fieldNames[i].toLowerCase() == latNames[j]){

                for (var a = 0; a < namesLength; a++) {
                    var option = {};
                    option.label = fieldNames[i];
                    option.value = i+"";
                    if(i==a){
                        option.selected = true;
                    }
                    optionsArr.push(option);
                }
                    match = true;
            }
        }
    }
    if(match){
        var drop1 = dijit.byId("selectLatitude");
        drop1.addOption(optionsArr);
    }else{
        var drop1 = dijit.byId("selectLatitude");
        drop1.addOption(options);//options is an array of options created originally
    }

回答by setlio

I found it, it is selector.attr("value", "the_name_of_the_option");

我找到了,它是 selector.attr("value", "the_name_of_the_option");