开发者

dijit.byId() returns undefined even in dojo.addOnLoad()

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开发者_StackOverflow 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.


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 by dijit.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.


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:

<script type="text/javascript" src="dojo/dojo.js"
        djConfig="parseOnLoad: true">
</script>

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜