dijit.byId() 即使在 dojo.addOnLoad() 中也返回未定义
我最近开始在道场工作。 即使我将此调用放在 dojo.onLoad() 内,dijit.byId() 也会返回未定义。
<script type="text/javascript">
dojo.require("dijit.form.Select");
function myhandler(ev)
{
var mydiv = dojo.byId('mydivid');
if (ev == 'Disable') {
mydiv.style.display = 'none';
} else {
mydiv.style.display = 'block';
}
}
function regHandler()
{
var item = dijit.byId("myState");
alert(item);//shows undefined
dojo.connect(dijit.byId("myState"), 'onChange', myhandler);
}
dojo.addOnLoad(regHandler);
</script>
<select input class="dialogInputElement" dojoAttachPoint="myState" dojoType=dijit.form.Select name="myState" id="myState" value='Enable' style="margin-bottom:5px">
<option value="Enable">Enable</option>
<option value="Disable">Disable</option>
</select>
如果我用 setTimeout() 延迟 dijit.byId() 调用,它就可以正常工作。 道场版本是1.4.2 任何解决方案或解决方法将不胜感激。
提前致谢。
i have recently started to work in dojo.
dijit.byId() returns undefined even if i put this call inside dojo.onLoad().
<script type="text/javascript">
dojo.require("dijit.form.Select");
function myhandler(ev)
{
var mydiv = dojo.byId('mydivid');
if (ev == 'Disable') {
mydiv.style.display = 'none';
} else {
mydiv.style.display = 'block';
}
}
function regHandler()
{
var item = dijit.byId("myState");
alert(item);//shows undefined
dojo.connect(dijit.byId("myState"), 'onChange', myhandler);
}
dojo.addOnLoad(regHandler);
</script>
<select input class="dialogInputElement" dojoAttachPoint="myState" dojoType=dijit.form.Select name="myState" id="myState" value='Enable' style="margin-bottom:5px">
<option value="Enable">Enable</option>
<option value="Disable">Disable</option>
</select>
it works fine if i delay dijit.byId() call with setTimeout().
dojo version is 1.4.2
Any solution or workaround will be appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这是您代码中的逐字记录,则看起来您在
dojoType
声明周围缺少双引号。如果您的输入从未声明为 dijit 小部件,则dijit.byId()
永远无法检索它。如果您想要一个更通用的节点选择器,请使用
dojo.byId()
,它将选择具有 id 的任何节点,而不仅仅是 dijit 小部件。If this is verbatim from your code, it looks like you are missing double-quotes from around your
dojoType
declaration. If your input is never declared as a dijit widget, it will never be retrievable bydijit.byId()
.If you want a more general node selector, use
dojo.byId()
instead, it will select any node with an id, not just dijit widgets.您是否尝试过将属性 djConfig="parseOnLoad: true" 添加到 dojo 配置中?
当您包含 dojo.js 库文件时,您可以设置如下属性:
在我的测试中,这似乎解决了问题。在此 jsfiddle 中,如果删除 djconfig 标签属性(在左侧导航栏上)并运行它,您会得到你所描述的行为。
Have you tried adding the attribute djConfig="parseOnLoad: true" to the dojo configuration?
When you include the dojo.js library file you can set the attribute like this:
In my tests, this seems to solve the problem. In this jsfiddle note if you remove the djconfig tag attribute (on the left nav bar) and run it, you get the behavior you are describing.