当表单包含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>
答案 0 :(得分: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
- dijit / 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