javascript 如何使用dojo动态创建下拉框的新选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5350525/
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
how do dynamically create new option of drop down box using dojo
提问by jslearner
JavaScript code:
JavaScript 代码:
var ref =dojo.byId("xyz");
var optn = document.createElement("OPTION");
optn.text="txt"
optn.value="val"
ref.options.add(optn);
I want dojo equivalent of above code
我想要 Dojo 相当于上面的代码
回答by Cu7l4ss
i think that would be:
我认为那将是:
var ref = dojobyId("xyz");
dojo.create("option", { value: "some", innerHTML: "label of option"}, ref);
just read the documentationabout the dom functions of dojo, it's simple enough.
只需阅读有关 dojo 的 dom 功能的文档,就足够简单了。
Good luck!
祝你好运!
回答by Stephen Chung
You are already using Dojo (dojo.byId).
您已经在使用 Dojo (dojo.byId)。
Dojo does not replace JavaScript (it augments it). When things are already clear or concise in JavaScript, it doesn't try to provide an alternative to the normal JavaScript way of doing it.
Dojo 不会取代 JavaScript(它增强了它)。当 JavaScript 中的事情已经很清楚或简洁时,它不会尝试提供常规 JavaScript 执行方式的替代方案。
In your example, you may try:
在您的示例中,您可以尝试:
dojo.create("option", { text:"txt", value:"val" }, dojo.byId("xyz"));
This creates the <option>
tag directly inside the <select>
element (which I assume is "xyz"). However, some browsers don't seem to like adding <option>
tags directly like this instead of adding it to the options
property. If so, you can use the add
function of the <select>
tag itself:
这会<option>
直接在<select>
元素内部创建标签(我假设是“xyz”)。但是,有些浏览器似乎不喜欢<option>
像这样直接添加标签,而不是将其添加到options
属性中。如果是这样,您可以使用标签本身的add
功能<select>
:
dojo.byId("xyz").add(dojo.create("option", { text:"txt", value:"val" }));
Notice that other than the call to dojo.create
that simpifies creating elements, everything related to adding an option to a <select>
is standard JavaScript.
请注意,除了调用dojo.create
简化创建元素之外,与向 a 添加选项相关的所有内容<select>
都是标准 JavaScript。
回答by Alfredo Carrillo
As innerHTML
doesn't work in IE, I did as follows, it worked:
由于innerHTML
在 IE 中不起作用,我做了以下工作,它起作用了:
option = document.createElement('option'); option.text = 'xyz'; option.value = 'val'; dojo.byId('commodities').add(option);
回答by ivandov
dijit.byId('mySelect').addOption({ label: 'text' , value: 'val' });
Worked for me in Dojo 1.6
在 Dojo 1.6 中对我来说有效
回答by Marcelo
Two options:
两种选择:
Option 1:
选项1:
dojo.require("dijit.form.Select");
var ref = dojo.byId("xyz");
dojo.create("option", {
value: "val",
innerHTML:
"label of option"
}, ref);
Option 2:
选项 2:
dojo.require("dijit.form.Select");
dojo.ready(function() {
new dijit.form.Select({
name: 'myName',
options: [{{
label: 'xyz',
value: 'val',
selected: true
}]
}).placeAt(dojo.byId("xyz"));
});