1

当表单包含 DateTextBox 时,我无法获取对 dijit 表单小部件的引用。下面的代码片段演示了这个问题。执行时,警告框显示“未定义”。但是,如果我摆脱<input ... id="dateTextBox"... />,我能够获得对表单小部件的引用。

<!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>
4

1 回答 1

2

我建议将其包装registry.byId成一个ready电话。

保留parse onLoad: true,删除

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

因为解析器将自动要求(当 dojo>= 1.8 时)并使用以下内容:

<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>

注意等待dojo/domReady!使用小部件时,触发通常是不够的。在加载并执行以下模块之前,不应初始化或访问许多小部件:

  • 道场/uacss
  • 迪吉特/hccss
  • 道场/解析器

因此,在使用小部件时,您通常应该将代码放在 dojo/ready() 回调中

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

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

于 2013-08-23T20:48:43.180 回答