Programatically change selected option of a Dojo Form Select that is populated by label value pair object

StackOverflow https://stackoverflow.com/questions/16205699

  •  11-04-2022
  •  | 
  •  

Pregunta

I have a Dojo Form Select input box where I fill the options using a javascript data object having lable value pairs (example code to build such object below):

for loop {
  varStateValuePairs.push({
    label: <State ID>, 
    value: <State Name>
  });
}
dijit.byId("StateDDL").addOption(varStateValuePairs);

Now, I want to programmatically select a specific State within this Dojo Form Select. I have tried the following:

dijit.byId("StateDDL").attr("value", String(5)); // 5 is the example value corresponding to the label-value pair I want to select
dijit.byId("StateDDL").attr("value", 5);
dojo.byId("StateDDL").value = 5;
dijit.byId("StateDDL").set("displayedValue", "Texas");

None of the above works. Where am I wrong? I have searched quite a lot and none of the solutions listed in other posts are working for me. I am running Dojo 1.8.

¿Fue útil?

Solución

Use Select.setValue().

http://jsfiddle.net/fiddlegrimbo/qauHX/2/

value0 would be selected by default, we select value2 manually.

var varStateValuePairs = [];
for (var i = 0; i < 10; i++) {
  varStateValuePairs.push({
    label: "state"+i, 
    value: "value"+i
  });
}

require(["dojo/parser", "dijit/registry", "dijit/form/Select", "dojo/domReady!"], function (parser, registry) {
    parser.parse().then(function () {
        var widget = registry.byId("StateDDL");
        widget.addOption(varStateValuePairs);
        widget.setValue("value2");
    });
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top