admin管理员组

文章数量:1305843

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.

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.

Share Improve this question edited Aug 24, 2011 at 17:46 user469635 asked Aug 24, 2011 at 17:36 user469635user469635 591 gold badge2 silver badges5 bronze badges 1
  • I don't believe Dojo's onLoad event guarantees that dijits have been parsed and instantiated (and hence are available to dijit.byId). Do you have parseOnLoad: true in your dojoConfig? What happens if you add dojo.parser.parse(); to your regHandler (before the rest of its code)? – Frode Commented Aug 25, 2011 at 10:18
Add a ment  | 

2 Answers 2

Reset to default 5

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.

本文标签: javascriptdijitbyId() returns undefined even in dojoaddOnLoad()Stack Overflow