简体   繁体   English

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

[英]how do i set selected in Dojo form.select option

Hi there Dojo developers, I have a drop down form.select, and it has few options, how do set an option to be selected. 您好Dojo开发人员,我有一个下拉form.select,它有几个选项,如何设置选项被选中。 Say I want to have the third option displayed in the select element. 假设我想在select元素中显示第三个选项。 I was looking at the dojo docs and I do not see setSelected() or similar. 我正在查看dojo文档,我没有看到setSelected()或类似的东西。

Thanks 谢谢

You need to use displayedValue property in addition to value to set the displayed option. 除了value之外,还需要使用displayedValue属性来设置显示的选项。 Use something like: 使用类似的东西:

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. 希望有所帮助。

我发现它,它是selector.attr(“value”,“the_name_of_the_option”);

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. 然而,我发现了我的错误:我正在动态创建选项,当我将其添加到选择器时设置.selected = true后,它会将状态更改为所选的第一个选项。 Or if I apply selector.set("displayedValue", "the_text_of_the_option"); 或者,如果我应用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. 它在视觉上显示所选择的一个,但实际上在所选的后面仍然是第一个如果我用上面的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. 这样当我添加它时,字母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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dojo形式选择不正确设置值 - Dojo form.Select not setting value properly 如何通过onclick使用f:form.select: - How to use f:form.select with onclick: 如何将数据传递到选项中<form.select></form.select>与语义 UI 反应? - How to pass data into options in <Form.Select> </Form.Select> with Semantic UI React? 从 Dojo 中的 Checked Multi Select (“dojox.form.CheckedMultiSelect”) 中选择选项时,如何编写条件? - How to write a condition when an option is selected from Checked Multi Select (“dojox.form.CheckedMultiSelect”) in Dojo? 加载页面后如何设置选择选项“已选择”? - how do set select option 'selected' after load page? 如何根据先前表格的选择将选择表格设置为选项? - How do I set a select form to an option, based on the selection of a previous form? 如何制作已从 select 中选择的选项的工具提示 - How do I make a tooltip of the option already selected from a select 如何在已禁用和已选中的选择中设置选项的样式? - How do I style an option in a select that is both disabled and selected? 如果选择了 select 标记中的选项,我如何创建元素 - How do I create an element if the option in select tag is selected 以编程方式更改由标签值对对象填充的Dojo Form Select的选定选项 - Programatically change selected option of a Dojo Form Select that is populated by label value pair object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM