[英]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.