我正在慢慢构建包含表单的弹出对话框的功能。它有一个例外。在按钮上的onClick事件中,我想更新表单中的值。它会更新值,但仅限于输入到表单中的div(emailForm)。我有div设置显示所以我可以看到更新。如何在myDialog对象中更新它?
<div id="emailForm" Executive Director>
<h1>Contact Form</h1>
<p>Feel free to drop us a line.</p>
<form id="contact-form" action="confirmation.php" method="post">
<fieldset class="contact-fieldset">
<ol>
<li>
<label for="to">To:</label>
<input id="to" name="to" type="text" size="40" value="Staff" disabled>
</li>
<li>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
</li>
</ol>
<input type="submit" class="contact-submit" value="Send">
</fieldset>
</form>
</div>
<script>
require(["dojo/dom",
"dijit/Dialog",
"dijit/form/TextBox",
"dijit/form/Textarea",
"dojo/domReady!"],
function(dom, Dialog, TextBox, TextArea)
{
toText = new TextBox({readOnly: "readOnly" , value: "Staff"},"to");
messageText = new TextArea({placeholder: "Your message"},message);
var emForm = dom.byId("emailForm").innerHTML;
myDialog = new Dialog({
title: "My Dialog",
content: emForm,
style: "width: 600px"
});
});
require(["dijit/form/Button",
"dojo/domReady!"],
function(Button)
{
var showEmailBtn = new Button({
onClick: function(evt){
toText.set("value","Executive Director");
myDialog.show(); } },
"showEmail");
});
</script>
更新
根据tik27对问题的描述,我尝试了以下测试:
<script>
require(["dojo/dom",
"dijit/Dialog",
"dijit/form/TextBox",
"dijit/form/Textarea",
"dojo/domReady!"],
function(dom, Dialog, TextBox, TextArea)
{
var emForm = '<div id="emailForm" Executive Director><h1>Contact Form</h1><form id="contact-form" action="confirmation.php" method="post"><fieldset class="contact-fieldset"><ol><li><label for="to">To:</label><input id="to" name="to" type="text" size="40" value="Gereral" disabled></li></ol><input type="submit" class="contact-submit" value="Send"></fieldset></form></div>';
myDialog = new Dialog({
title: "My Dialog",
content: emForm,
style: "width: 600px"
});
toText = new TextBox({readOnly: "readOnly" , value: "Staff"},"to");
});
require(["dojo/dom",
"dijit/form/Button",
"dojo/domReady!"],
function(dom, Button)
{
var showEmailBtn = new Button({
onClick: function(evt){
toText.set("value","Executive Director");
myDialog.show(); } },
"showEmail");
});
</script>
这解决了这个问题。显示的html只是对完整表单的一个小测试。如何将html与脚本分开,然后在不重复id的情况下将其带入?我查看了模板,但这似乎有些过分。
更新2
再次,根据Tik27的一些建议,我试过这个:
<script id="emailHTML" type="text/template">
<div id="emailForm" Executive Director>
<!-- Same html as above in emailForm -->
</div>
</script>
<script>
require(["dojo/dom",
"dijit/Dialog",
"dijit/form/TextBox",
"dijit/form/Textarea",
"dojo/domReady!"],
function(dom, Dialog, TextBox, TextArea)
{
myDialog = new Dialog({
title: "My Dialog",
content: dom.byId("emailHTML").textContent,
style: "width: 600px"
});
toText = new TextBox({readOnly: "readOnly" , value: "Staff"},"to");
});
require(["dojo/dom",
"dijit/form/Button",
"dojo/domReady!"],
function(dom, Button)
{
var showEmailBtn = new Button({
onClick: function(evt){
toText.set("value","Executive Director");
myDialog.show(); } },
"showEmail");
});
</script>
浏览器忽略文本/模板脚本中的任何内容,因此id仅添加一次到DOM。在此站点上搜索有关脚本类型的更多信息。
答案 0 :(得分:0)
这里的问题与你这样做的方式是,最终会输入id =“to”。 Ids在html中应该是唯一的。如果你想这样做,我建议你把你的表单信息放到一个带有type =“text / template”的脚本标签中。
但是你有没有理由不以声明方式创建它?
<div id="emailForm" data-dojo-type="dijit.Dialog" style="display:none">
<h1>Contact Form</h1>
<p>Feel free to drop us a line.</p>
<form id="contact-form" action="confirmation.php" method="post">
<fieldset class="contact-fieldset">
<ol>
<li>
<label for="to">To:</label>
<input data-dojo-type="dijit.form.TextBox" data-dojo-props="readOnly: 'readOnly' , value:'Staff'" id="to" name="to" type="text" size="40">
</li>
<li>
<label for="message">Message:</label>
<textarea id="message" name="message" data-dojo-type="dijit.form.Textarea" data-dojo-props="placeholder: 'Your message'"></textarea>
</li>
</ol>
<input type="submit" class="contact-submit" value="Send">
</fieldset>
</form>
</div>
<div id="showEmail">Show</div>
<script>
require(["dijit/form/Button",
"dijit/registry","dojo/dom",
"dijit/Dialog",
"dijit/form/TextBox",
"dijit/form/Textarea"],
function(Button,registry)
{
var showEmailBtn = new Button({
onClick: function(evt){
registry.byId("emailForm").show();
registry.byId("to").set("value","Executive Director");
} }, "showEmail");
});
</script>