如何在网页中移动dojo脚本?

mpbci0fu  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(169)

我有一个正在运行的JavaScript页面,它使用dojo检查dijit/Dialog表单的结果。

<div class="content">
    <div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
<form data-dojo-type="dijit/form/Form" data-dojo-id="myForm">
    <script type="dojo/on" data-dojo-event="submit" data-dojo-args="e">
        require(["dijit/registry"],function(registry){
            e.preventDefault(); // prevent the default submit
            if(!myForm.isValid()){ alert("Please fix fields"); return; }
            var inputField = registry.byId("input-field");
            var resultVal = inputField.get("value");
            console.log("Would submit "+resultVal+" here via dojo/xhr.");
            window.alert("Would submit "+resultVal+" here via dojo/xhr.");
            myFormDialog.hide();
        });
    </script>
    <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/TextBox" />
    </div>
    <div class="dijitDialogPaneActionBar">
            <button data-dojo-type="dijit/form/Button" type="submit">OK</button>
            <button data-dojo-type="dijit/form/Button" type="button"
                data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
    </div>
 </form>
</div>
<p>When pressing this button the dialog will popup:</p>
<button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
<script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
    myFormDialog.show();
</script>
</button>

</div>
<script>
  require(["dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/TextBox", "dojo/on"]);';
</script>

我想将表单中的脚本移到页面的底部,这样我就可以将它与另一个页面集成在一起。
(This代码由PHP生成,我使用heredoc来包含来自dojotools示例的代码。)
我尝试了以下代码:

<div class="content">
<div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
<form data-dojo-type="dijit/form/Form" data-dojo-id="myForm">
    <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" data-dojo-id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/TextBox" />
    </div>
    <div class="dijitDialogPaneActionBar">
            <button data-dojo-type="dijit/form/Button" type="submit" data-dojo-id="my-button" id="my-button" data-dojo-args="e">OK</button>
            <button data-dojo-type="dijit/form/Button" type="button"
                data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
    </div>
 </form>
</div>

<p>When pressing this button the dialog will popup:</p>
<button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
  <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
      myFormDialog.show();
  </script>
</button>
</div>
<script>
require(["dojo/dom", "dojo/on", "dijit/registry", "dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/TextBox", ],
function(dom, on, registry){
  on(dom.byId("my-button"), "click", function(e){
      e.preventDefault(); // prevent the default submit
      console.log("Reached on() function.");
  })
});
</script>

这会在页面上产生以下结果(从Chrome开发者工具复制,其中控制台不显示任何语法错误):

<div class="content">
  <div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
    <form data-dojo-type="dijit/form/Form" data-dojo-id="myForm">
      <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" data-dojo-id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/TextBox" />
      </div>
      <div class="dijitDialogPaneActionBar">
        <button data-dojo-type="dijit/form/Button" type="submit" data-dojo-id="my-button" id="my-button" data-dojo-args="e">OK</button>
        <button data-dojo-type="dijit/form/Button" type="button"
          data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
      </div>
    </form>
  </div>
  <p>When pressing this button the dialog will popup:</p>
  <button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
    <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
      myFormDialog.show();
    </script>
  </button>
</div>
<script>
require(["dojo/dom", "dojo/on", "dijit/registry", "dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/TextBox", ],
function(dom, on, registry){
  on(dom.byId("my-button"), "click", function(e){
      e.preventDefault(); // prevent the default submit
      console.log("Reached on() function.");
  })
});</script>

在这种情况下,on()处理程序似乎没有被调用。Show Me按钮出现。
如果你点击它,它会显示带有Foo字段的表单。如果你在字段中输入一个值,然后点击OK按钮,表单会消失。但是没有消息发布到控制台。
有人能解释一下为什么脚本可以作为表单的一部分工作,而不是作为页面上的一个单独调用吗?
原始代码来自dijit/对话框页面(https://dojotoolkit.org/reference-guide/1.10/dijit/Dialog.html#dijit-dialog)的“对话框中的表单和功能”部分。

nsc4cvqm

nsc4cvqm1#

首先,on(dom.byId("my-button"), "click", function(e){...})将不会触发,因为它附加到一个dom,而dom未被dojo解析器替换,
您还使用data-dojo-idid声明了按钮,
注意data-dojo-id(此处命名时不要使用减号-,而是使用下划线_)在窗口对象中创建了一个具有此ID的全局元素,因此以后您可以像此处my_button.on(...)一样直接访问您的元素
而通过id,它创建具有widgetid=id的小部件,以便通过dijit/registry访问它。
第二点:重要的是,为了访问这些小部件,您应该等到它们都被解析完,因此使用回调函数dojo/ready to be sur确保所有小部件都被正确解析,
因此,在您的情况下,在就绪后(小部件已解析,没有生成dom项,id="my-button"被小部件本身替代),因此提交按钮将直接提交,没有preventdefault操作,也没有消息
参见以下工作片段:

<script type="text/javascript">
  dojoConfig = {
    isDebug: true,
    async: true,
    parseOnLoad: true
  }
</script>

<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet" />


<body class="claro">
  <div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
    <form data-dojo-type="dijit/form/Form" data-dojo-id="myForm" action="#">
      <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" data-dojo-id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/ValidationTextBox"/>
      </div>
      <div class="dijitDialogPaneActionBar">
        <button data-dojo-type="dijit/form/Button" type="submit" data-dojo-id="my_button" id="my-button" data-dojo-args="e">OK</button>
        <button data-dojo-type="dijit/form/Button" type="button" data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
      </div>
    </form>
  </div>

  <p>When pressing this button the dialog will popup:</p>
  <button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
  <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
      myFormDialog.show();
  </script>
</button>
  <script>
    require(["dojo/dom", "dojo/on", "dijit/registry", "dojo/ready", "dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/ValidationTextBox"],
      function(dom, on, registry, ready) {

        ready(function() {
          // my_button same as = registry.byId("my-button");
          my_button.on("click", function(e) {  
            e.preventDefault(); // prevent the default submit
            console.log("Reached on() function.",myForm.validate());
            if(myForm.validate()) {
              //do som stuff here and then close dilaog
            }
          });
          
        });

      });
  </script>


</body>

相关问题