如何在JQuery UI对话框中设置元素的值

时间:2013-02-10 08:06:21

标签: jquery ajax jquery-ui

我目前遇到一个问题,我需要将值传递给Jquery UI对话框,但我不知道如何。我到处查看文档,但似乎没有任何效果。这就是我所拥有的(并且已经尝试过)

实际对话:

    <div id="amountDialog" title="Add to Basket">
        <table style="text-align: center">
            <tr>
                <td colspan="2">
                    <p id="productAdd"></p> <!-- need to set this value -->
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" id="modalInputAmount" />
                </td>
                <td>
                    <p id="maxAmt">Maximum Amount: </p>
                </td>
            </tr>
        </table>
    </div>

对话框选项:

$( "#amountDialog" ).dialog({ 
                        autoOpen: false, 
                        width: 'auto',
                        modal: true,
                        buttons : {
                            "OK" : execute
                        },
                        open : function (event, ui) {
                            $("#productAdd").val('How many [' + item + ']\'s do you wish to add to the basket?"'); <!-- item is the global variable describing which item is added to the basket -->
                        }
                    });

打开代码

    $("#amountDialog" ).dialog("open");

我不打算在这里粘贴执行代码,因为我知道它有效,我可以从模态对话框中的文本框中获取值。但我担心的是,当模态对话框打开时,如何设置#productAdd段落的值?

只需要再把它放在那里,我仍然是JQuery的新手。

2 个答案:

答案 0 :(得分:4)

应为html()而不是val(),请更改:

$("#productAdd").val(...)

$("#productAdd").html(...);

答案 1 :(得分:0)

您的问题是#productAdd是一个段落(<p>)。 jQuery的val()方法仅适用于表单字段。请改用htmltext。例如。 $('#productAdd').text("How many ...")

另外,您想重新考虑如何确定#productAdd的新内容。您现在拥有的parent().parent().find('specific thing')表示您的脚本会在HTML更改后立即中断。