gpt4 book ai didi

javascript - Dojo 1.9 NumberTextBox 不工作?

转载 作者:行者123 更新时间:2023-11-28 19:53:35 25 4
gpt4 key购买 nike

我试图让 Dojo NumberTextBox 在我的代码中工作,但由于某种原因,当我将其“移植”到我的网站时,示例代码无法工作。我直接从 Dojo 1.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() block ,并且我认为它的位置正确(它显示在 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 代码生成的 HTML INPUT 标记:

                    <input id="yearsInBusinessOther" name="yearsInBusinessOther" data-dojo-props="constraints:{min:6,max:99,places:0}, invalidMessage:&#39;Invalid value.&#39;" data-dojo-type="dijit/form/NumberTextBox" type="text" value="99"/>

但是没有验证。我可以在表单字段中输入任何内容,但我永远不会收到错误消息、格式更改或其他任何表明正在触发任何验证的内容。

了解 Dojo 1.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 documentation :

Notice that dojoConfig is defined in a script block before dojo.js is loaded. This is of paramount importance—if reversed, the configuration properties will be ignored.

在这种情况下,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>

关于javascript - Dojo 1.9 NumberTextBox 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23000991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com