2014-04-10 60 views
0

我試圖讓我的代碼中的Dojo NumberTextBox工作,並且出於某種原因,當我將它「移植」到我的網站時,示例代碼不工作。我直接從Dojo 1.9文檔拉到這個例子的代碼,它的工作原理:Dojo 1.9 NumberTextBox不工作?

<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頁面。正如你所看到的,我有需要()塊和我覺得進口它的正確放置(它顯示在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> 

而這裏的HTML INPUT標記,上面的JSP代碼產生:

    <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的人可以看看這個並希望指出我做錯了什麼嗎?

回答

3

當使用dojoConfig配置道場,那麼你必須把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> 

什麼情況是,加載道場時,正在檢查在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> 
+0

謝謝!這些小東西總讓我把頭髮拉出來。 – user3120173

0

您可以通過使用djConfig="parseOnLoad:true"做同樣的道場配置在同一行。

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