0

我正在慢慢构建包含表单的弹出对话框的功能。它有一个例外。在按钮的 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 一次。在此站点上搜索有关脚本类型的更多信息。

4

1 回答 1

0

您执行此操作的方式的问题在于,最终会使用 id="to" 进行输入。id 在 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>

小提琴:: http://jsfiddle.net/ZJ735/

于 2013-07-26T05:14:38.003 回答