jQuery Dialog但获得动态输入

时间:2012-02-22 21:00:07

标签: php jquery

全部, 我正在尝试使用jQuery对话框模式窗口发送电子邮件。我可以让窗口正确填充和所有类似的东西,但是当我打开对话框窗口时,我正在尝试填充一些电子邮件地址。这是打开对话框窗口的jQuery:

$( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 600,
    width: 750,
    modal: true,
    show: 'slide',
    buttons: {
        "Send Email": function() {
                var message_to_send = $( "#message_to_vendors" ).val();
                alert(message_to_send);
                var vendor_ids = $("#email_vendor_ids").val();
                alert(vendor_ids);
                $.post("send_vendor_emails.php", { email_vendor_ids: vendor_ids, message_to_vendors: message_to_send }, function(data) {
                    alert("The data is: "+data);
                    if(data=="yes"){
                        $("#dialog-form").dialog( "close" );
                    }
               });

        },
        Cancel: function() {
            $( this ).dialog( "close" );
        }
    },
    close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
    }
});

然后是我的对话框:

<div id="dialog-form" title="Email Vendors">
<form>
<select data-placeholder="Vendors to Email" style="width:350px;" multiple class="chzn-select">
<div id="email_options">
<option selected>Email Address 1</option>
<option selected>Email Address 2</option>
</div>
</select>
</form>
</div>

我在会话变量中存储了一些电子邮件地址,而我想要发生的是当我单击对话框打开它时我可以填充Session变量中的电子邮件地址。如何使用对话框执行此操作?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

听起来您只需将会话中的电子邮件打印到对话框的标记中即可。您想要将它们添加为select元素的选项,对吗?

<div id="dialog-form" title="Email Vendors">
<form>
<div id="email_options">
<select data-placeholder="Vendors to Email" style="width:350px;" multiple class="chzn-select">

<?php foreach ( $_SESSION['emails'] as $e ): ?>
    <option><?php echo $e; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
</div>

假设电子邮件在$_SESSION['emails']中存储为数组。为了有效的XHTML,我还将你的email_options div移到选择之外。

<强>更新

由于您澄清了用户能够在加载页面后动态地向会话添加电子邮件,因此当您向会话添加电子邮件时,您应该使用一些JavaScript来保持会话和对话框的同步。像这样:

/* Where ever you update the session with more emails, also do this
Note, this is using the modified markup from my above example 
(select is child of #email_optons) */

var new_email = '';  // Set this to the value of the email which is being added to the session

$('#email_options select').append( $('<option>' + new_email + '</option>' ) );