Cannot get a reference to a dijit form when the fo

2019-08-08 18:03发布

I am having trouble getting a reference to the dijit form widget when the form contains a DateTextBox. The code snippet below demonstrates the problem. When executed, the alert box says "undefined". However, if I get rid of <input ... id="dateTextBox"... />, I am able to get a reference to the form widget.

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="//ajax.googleapis.com/ajax/libs/dojo/1.9.1/dijit/themes/claro/claro.css" media="screen">

        <!-- load dojo and provide config via data attribute -->
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js"
            data-dojo-config="async: true, parseOnLoad: true">
        </script>
        <script type="text/javascript">
            require(["dijit/form/TextBox", "dijit/form/DateTextBox"]);
        </script>

        <script type="text/javascript">
            require(["dojo/parser", "dijit/registry", "dijit/form/Form", "dojo/domReady!"],
            function(parser, registry) {
                parser.parse();
                alert(registry.byId("frm_test"));
            });
        </script>
    </head>

    <body class="claro">
        <div data-dojo-type="dijit/form/Form" id="frm_test" encType="multipart/form-data" action="" method="">  
            <input type="text" id="textBox" name="textBox" data-dojo-type="dijit/form/TextBox" />
            <input type="text" id="dateTextBox" name="dateTextBox" data-dojo-type="dijit/form/DateTextBox" />
        </div>
    </body>
</html>

1条回答
Fickle 薄情
2楼-- · 2019-08-08 18:53

I'd recommend wrapping the registry.byId into a ready call.

Keep parse onLoad: true, remove

require(["dijit/form/TextBox", "dijit/form/DateTextBox"]); 

as the parser will auto require (when dojo>= 1.8) and use the following:

<script type="text/javascript">
    require(["dojo/ready", "dijit/registry", "dojo/domReady!"],
        function(ready, registry) {
            // by default the prioirty of this ready call will be after the 
            // ready call used to parse when parseOnLoad is true
            ready(function() { 
                alert(registry.byId("frm_test"));
            });
    });
</script>

Note that waiting for dojo/domReady! to fire is often not sufficient when working with widgets. Many widgets shouldn’t be initialized or accessed until the following modules load and execute:

  • dojo/uacss
  • dijit/hccss
  • dojo/parser

Thus when working with widgets you should generally put your code inside of a dojo/ready() callback

http://dojotoolkit.org/reference-guide/1.9/dojo/domReady.html

http://dojotoolkit.org/reference-guide/1.9/dojo/ready.html#dojo-ready

查看更多
登录 后发表回答