我有一个工作的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>
我想将脚本以表格的形式移至页面底部,以便将其与另一个页面集成。
(此代码由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()
处理程序似乎没有被调用。出现显示我按钮。
如果单击它,它将显示带有Foo字段的表单。如果在该字段中输入一个值,然后单击确定按钮,该表单将消失。但是没有消息发布到控制台。
有人建议为什么脚本会作为表单的一部分而不是页面上的单独调用吗?
原始代码来自dijit /对话框页面(https://dojotoolkit.org/reference-guide/1.10/dijit/Dialog.html#dijit-dialog)的“对话框中的窗体和功能”部分。
答案 0 :(得分:0)
首先on(dom.byId("my-button"), "click", function(e){...})
不会触发,因为它被连接到一个不完全是dom的域,而后被dojo解析器替换,
您还同时声明了data-dojo-id
和id
的按钮
请注意,data-dojo-id
(此处不要使用负号-
,而应使用下划线_
)在window对象中创建一个具有此id的全局元素,因此以后您可以访问您的元素直接像这里my_button.on(...)
在id
期间,它使用widgetid=id
创建一个小部件,以便dijit/registry
对其进行访问。
第二个:重点是要在这里访问那些小部件,您应该等待它们全部解析完毕,因此请使用回调函数dojo / ready确保所有小部件都已正确解析,
准备好后,就可以了(小部件已解析,没有生成的id="my-button"
被小部件itselef替换的dom项目)
因此,提交按钮将直接提交,并且没有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>