【问题标题】:Dojo 1.9 NumberTextBox not working?Dojo 1.9 NumberTextBox 不工作?
【发布时间】:2014-05-24 22:11:02
【问题描述】:

我试图让 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() 块,我认为它被正确放置(它显示在 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 的人可以看看这个并(希望)指出我做错了什么吗?

【问题讨论】:

    标签: javascript jsp dojo dojo-1.9


    【解决方案1】:

    您可以使用djConfig="parseOnLoad:true" 在一行中完成相同的道场配置。

    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js.uncompressed.js" djConfig="parseOnLoad:true"></script>
    

    【讨论】:

      【解决方案2】:

      当使用dojoConfig 配置Dojo 时,您必须将dojoConfig 配置放在 加载dojo.js 之前。在文档中,您可以注意到它们以正确的顺序加载:

      <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

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

      【讨论】:

      • 谢谢!正是这些小事总是让我拔头发。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多