Cannot get a reference to a dijit form when the form has a DateTextBox

断了今生、忘了曾经 提交于 2019-12-13 20:02:53

问题


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:


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



来源:https://stackoverflow.com/questions/18411315/cannot-get-a-reference-to-a-dijit-form-when-the-form-has-a-datetextbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!