在弹出框上循环ajax的jQuery问题

时间:2013-03-02 20:17:47

标签: jquery ajax forms

我有一个奇怪的问题,我希望你能帮我找出我做错了什么。

我有一个名为“弹出”的div和“ send_mail ”按钮。

按下“ send_mail ”按钮后,“弹出窗口”会显示ajax邮件表单和“关闭”按钮。

当我填写邮件表格并发送邮件时,邮件将发送到我的电子邮件地址。

当然,在这一部分,一切都完美无瑕。到目前为止一切都很好......

问题是当我打开“弹出”表单时,使用“关闭”按钮关闭它,然后再次重新打开表单并发送邮件。发生的事情是我在邮箱上收到两次相同的邮件。

显然,当我打开并关闭弹出窗口10次并且第10次发送电子邮件时,我会在邮箱上收到10封邮件......

我能解决此问题并只发送一封邮件的唯一方法是刷新整个页面。

可能是什么?

这是js代码(我很抱歉混乱的形式):

第一行是确定按下哪个按钮,因为主html页面中有多个按钮(取决于用户数量,每个用户都有一个“send_mail_X”按钮)。

$("#list_container").on('click', "[id^='send_mail']", function () {
    var val = $(this).attr("value");
    $.ajax({
        type: "POST",
        url: "include/list_class.php",
        data: val,
        success: function(data){

            $("#Popup").on('click', "#close", function () {
                disablePopup();
            });


            $("#Popup").on('submit', "#admin_mail", function () {

                var sendbutton = $(this).find('#sendmail');
                var action = "include/admin_contact.php";
                sendbutton.attr('disabled','disabled');

                $.post(action, {
                    email: $("#Popup").find('#email').val(),
                    title: $("#Popup").find('#title').val(),
                    message: $("#Popup").find('#message').val()
                },
                    function(data){
                        document.getElementById('admin-form_output').innerHTML = data;  
                        centerPopup();
                    }
                );


                return false;



            });


            var array = data.split('#');
            var mymail = array[0];
            var myaddress = array[1];
            var isdisabled = "";
            if(mymail == ""){
                mymail = "Nie można wyslać maila.<br>Brak ardesu email.";
                isdisabled = "disabled='disabled'";
            }
            if(myaddress == ""){
                myaddress = "Uzytkownik nie podał swojego adresu.<br>Zaznacz jego zamowienie na czerwono.";
            }


            $("#bgPopup").data("state",0); 
            loadPopup(); 

            $("#Popup").on('click', "#popupClose", function () {  
                disablePopup();  
                return false;
            }); 



            $("#Popup").html("<a href='#' id='popupClose'><img src='images/close.png'></img></a> <div class='large-notice'><div id='admin-form_output'></div><br /><h4>ADRES WYSYLKOWY:</h4>" + myaddress + "<br /><br /><br /><h4>KONTAKT MAIL:</h4><div id='admin-form'><div class='field' style='display: block;'><form name='admin_mail' id='admin_mail' method='post' action='include/admin_contact.php' ><input type='hidden' name='email' id='email' value='" + mymail + "'/> <input type='text' name='title' id='title' onFocus='if(this.value == \"Tytul wiadomosci\") { this.value = \"\"; }' onBlur='if(this.value == \"\") { this.value = \"Tytul wiadomosci\"; }' value='Tytul wiadomosci' /> <textarea name='message' cols='190' rows='6' id='message' onFocus='if(this.value == \"Wiadomosc\") { this.value = \"\"; }' onBlur='if(this.value == \"\") { this.value = \"Wiadomosc\"; }' value='Wiadomosc'>Wiadomosc</textarea><div class='button'> <br /><input type='submit' class='btn-image' id='sendmail' " + isdisabled + " value='Wyslij' /></div><div class='button'> <br /><input type='button' class='btn-image' id='close' value='Zamknij' /></div></form></div></div><br /><br /><br />" + mymail + "</div>");

            centerPopup();

        }   

    });                                                                 


    return false;
});


function loadPopup(){  
    //loads popup only if it is disabled  

    if($("#bgPopup").data("state")==0){  
        $("#bgPopup").css({  
            "opacity": "0.7"  
        });  
        $("#bgPopup").fadeIn("medium");  
        $("#Popup").fadeIn("medium");
        $("#bgPopup").data("state",1);  
    }  
}  

function disablePopup(){
    $("#Popup").empty();
    if ($("#bgPopup").data("state")==1){  
        $("#bgPopup").fadeOut("medium");  
        $("#Popup").fadeOut("medium");  
        $("#bgPopup").data("state",0);  
    }  
}  

function centerPopup(){ 


    var winw = $(window).width();  
    var winh = $(window).height();  
    var popw = $('#Popup').width();  
    var poph = $('#Popup').height();  
    $("#Popup").css({  
        "position" : "fixed",  
        "top" : winh/2-poph/2,  
        "left" : winw/2-popw/2  
    });  
    //IE6  
    $("#bgPopup").css({  
        "height": winh    
    });  
}

1 个答案:

答案 0 :(得分:0)

将此功能放入关闭按钮的onclick事件

function empty_div(){
    $("#yourmaindiv").html('');
}

它将做什么是清空包含表格及其元素的div。

另一种可能的解决方案是在表单名称中使用随机数。因此,每次加载新表单时,它将具有唯一的id和name属性,并且您只提交该表单而不是所有表单。我认为每次加载表单时,它们都具有相同的名称并导致问题

<-form name='myForm2322343233' id='myform2322343233' method='post'    action='somwhere.php'->

所以每次它都有唯一标识符