Ajax发送2个POST请求...我没有任何live()事件处理程序

时间:2011-03-29 16:46:13

标签: jquery

我想根据点击事件(登录和注册)发送两个异步请求中的一个。 我的点击处理程序为每个相应的点击发送2个POST请求(2个登录请求或2个注册请求)。我知道live方法创建的事件处理程序可能会导致此问题。问题是,我的脚本中没有任何内容。我用.delegate()

替换了它们

这是......

 //ajax registration.  
$('input#register_submit').click(function(event){
    $.post("/register_async/", $('div#register_side form').serialize(), 
        function(data){
            $.each(data,function(){
            if(this.user_status==1){
                $('#header-top').html('<ul><li>Hi,'+ this.user_fname + '&nbsp | </li><li><a href="">Log Out</a></li></ul>');
                $('#post_login_modal').dialog("close");

                $('input[name=preview]').unbind('click');

                $('a.login').unbind('click');
                $('li a.account').unbind('click');  

            }
            else{
                 $('p#login_title').text('There are errors in your form').css("color","red");

            //redirect upon failed registration
            //window.location='{{site}}register';

            }
        });         
        }
    ,'json');
    event.stopPropagation();
    return false;
    });


//ajax login...just for this page right now  
$('input#login_submit_btn').click(function(event){
    $.post("/login_async/", $('div#login_side form').serialize(), 
        function(data){
            $.each(data,function(){
            if(this.user_status==1){
                $('#header-top').html('<ul><li>Hi,'+ this.user_fname + '&nbsp | </li><li><a href="">Log Out</a></li></ul>');
                $('#post_login_modal').dialog("close");

                $('a.login').unbind('click');
                $('li a.account').unbind('click');

                //specific to upload page.  removes login/register modal window click handler
                $('input[name=preview]').unbind('click');

                $('#contact_btn').unbind('click');
                $('#contact_btn').click(function(){
                $('#contact_renter').dialog('open');
                    });

                $('#save_btn').unbind('click');
                $('#save_btn').click(function(){
                    $('#save_modal').dialog('open');
                });


                $('span.anon').unbind('click');
                $('li.star_rater span.last').click(function(e){
                    $('#rate_modal').dialog('open');
                    e.stopPropagation();

                $('#flag span').click(function(event){
                    $('#rate_modal').dialog('close');
                    $('#flag_modal').dialog('open');
                    event.stopPropagation();

                });     

                //change rating/reporting, saving favorites, and contact based on ajax response 

            });

            }
            else{
                $('p#login_title').text('That email/password is invalid').css("color","red");
            }
            //prevents user from saving post as favorite if he has already done so
            if(this.user_favorite==1){
                $('#stats li:eq(3)').remove();
                $('<li><span id="saved">Saved<span></li>').appendTo('ul#stats');
            };  
            //prevents user from rating/reporting more than once
            if(this.flag_record==1){
                $('.star_rater span.anon').remove();
                $('<span class="voted"> Rated! </span>').appendTo('li.star_rater');
            };  

        });
    },'json');
    return false;
    });

要处理关闭我网站中的所有模态窗口,我也有这个事件处理程序:

   $('body').delegate(".ui-widget-overlay","click", function(e) {
$("#rate_modal,#contact_renter,#save_modal, #login_modal,#post_login_modal,#flag_modal,#save_success,#flag_success,#mail_success,#rating_success,#delete_note").dialog("close");
e.stopPropagation();
});

我可以发誓,在我将实时处理程序更改为委托后,它起作用了。

1 个答案:

答案 0 :(得分:2)

我不知道为什么我之前没想过这个。如果这些点击处理程序附加到<input type="submit">按钮,那么您将提交表单两次:一次是针对点击事件,一次是通过AJAX,另一次是通过表单的onsubmit事件。

将您的.click()功能更改为.submit()并将其附加到表单元素而不是提交按钮。

$('form#form1').submit( function() {
    // your existing code
    return false;
}

或者将这些提交按钮更改为实际的<button><a> s或其他内容。