我正试图让Dojo NumberTextBox在我的代码中工作,但由于某种原因,当我将其"移植"到我的站点时,示例代码不起作用。我直接从Dojo1.9文档中提取了这个示例代码,它很有效:
<html >
<head>
<link rel="stylesheet" href="../../_static/js/dojo/../dijit/themes/claro/claro.css">
<script>dojoConfig = {async: true, parseOnLoad: true}</script>
<script src='../../_static/js/dojo/dojo.js'></script>
<script>
require(["dojo/parser", "dijit/form/NumberTextBox"]);
</script>
</head>
<body class="claro">
<label for="q05">Integer between -20000 to +20000:</label>
<input id="q05" type="text"
data-dojo-type="dijit/form/NumberTextBox"
name= "elevation"
required="true"
value="3000"
data-dojo-props="constraints:{min:-20000,max:20000,places:0},
invalidMessage:'Invalid elevation.'" />
</body>
</html>
这是我的JSP代码,它从一系列JSP页面开始。正如你所看到的,我导入了require()块,我认为它被正确放置(它显示在HTML页面的头部):
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js.uncompressed.js"></script>
<script src="${pageContext.request.contextPath}/js/mystuff.js"></script>
<script type="text/javascript" >
dojoConfig={async: true, parseOnLoad: true};
require(["dojo/parser", "dijit/form/NumberTextBox"]);
</script>
<form:form id="formInfo" commandName="formInfo" action="${flowExecutionUrl}" enctype="multipart/form-data">
<div id="YIBOtherContainer" style="display:none;"> <%-- hidden to start with --%>
<form:input id="yearsInBusinessOther" path="yearsInBusinessOther"
data-dojo-type="dijit/form/NumberTextBox"
data-dojo-props="constraints:{min:6,max:99,places:0}, invalidMessage:'Invalid value.'" />
<div class="formRow otherIndent">
<form:errors cssClass="formError" path="yearsInBusinessOther" />
</div>
</div>
</form:form>
下面是上面JSP代码生成的HTMLINPUT标记:
<input id="yearsInBusinessOther" name="yearsInBusinessOther" data-dojo-props="constraints:{min:6,max:99,places:0}, invalidMessage:'Invalid value.'" data-dojo-type="dijit/form/NumberTextBox" type="text" value="99"/>
但还没有得到证实。我可以在表单字段中键入任何内容,而且我永远不会收到错误消息、格式更改或任何其他指示任何验证正在启动的信息。
了解Dojo1.9的人能看到这一点并(希望)指出我做错了什么吗?
使用dojoConfig
配置Dojo时,必须在加载dojo.js
之前放置dojoConfig
配置。在文档中,您可以注意到它们是按照正确的顺序加载的:
<script>dojoConfig = {async: true, parseOnLoad: true}</script><!-- First -->
<script src='../../_static/js/dojo/dojo.js'></script><!-- Second -->
但在你的例子中:
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js.uncompressed.js"></script><!-- First but should be second! -->
<script type="text/javascript">
dojoConfig={async: true, parseOnLoad: true}; // Second, but should be first!
require(["dojo/parser", "dijit/form/NumberTextBox"]);
</script>
发生的情况是,当Dojo被加载时,它正在检查dojoConfig
中配置的属性。但是,如果您颠倒顺序,那么它将不起作用,引用Dojo文档:
注意,dojoConfig是在dojo.js之前在脚本块中定义的加载。这一点至关重要——如果相反,配置属性将被忽略。
在这种情况下,parseOnLoad
将被忽略,这意味着您的输入字段永远不会转换为dijit/form/NumberTextBox
。
所以我建议把你的代码分成:
<script type="text/javascript">
dojoConfig={async: true, parseOnLoad: true};
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js.uncompressed.js"> </script>
<script type="text/javascript">
require(["dojo/parser", "dijit/form/NumberTextBox"]);
</script>
您可以使用djConfig="parseOnLoad:true"
在一行中完成相同的dojo配置。
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js.uncompressed.js" djConfig="parseOnLoad:true"></script>