简体   繁体   中英

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

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.

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");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM