如何跟踪jQuery对话框调用的操作



我有点进退两难:)

我有一个供用户投票的链接。单击链接会生成一个jQuery AJAX调用,检查该人是否已登录。如果没有,对话框将显示一个要登录的表单。

但问题是,jQuery调用登录和整个位与弹出框是在不同的地方。

我需要做的是检查用户是否成功登录,并更新投票计数。

我正在做这个网站:http://www.problemio.com

以下是目前为止我的jQuery代码:
<script type="text/javascript">
$(document).ready(function() 
{
     var $dialog = $('#loginpopup')
       .dialog({
         autoOpen: false,
         title: 'Login Dialog'
       }); 
        $("#newprofile").click(function () {
          $("#login_div").hide();
          $("#newprofileform").show();
        });

    $('.vote_up').click(function() 
    {        
        problem_id = $(this).attr("data-problem_id");
        var dataString = 'problem_id='+ problem_id + '&vote=+';
        $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data)
                {           
                    // ? :)
                    alert (data);   
                },
                error : function(data) 
                {
                    errorMessage = data.responseText;
                    if ( errorMessage == "not_logged_in" )
                    {
                        // Try to create the popup that asks user to log in.
                        $dialog.dialog('open');
                        // prevent the default action, e.g., following a link
                        return false;
                    }
                    else
                    {
                        alert ("not");
                    }
                    //alert(JSON.stringify(data));
                }
            });

        //Return false to prevent page navigation
        return false;
    });
    $('.vote_down').click(function() 
    {
        alert("down");
        problem_id = $(this).attr("data-problem_id");
        var dataString = 'problem_id='+ problem_id + '&vote=-';        
        //Return false to prevent page navigation
        return false;
    });    
});
</script>

除了在$dialog.dialog('open');行之后,它都可以工作-我不知道如何

  1. 获取失败成功的返回信号,但不知道具体如何
  2. 更新被投票的项目,因为它只是页面中可以投票的众多项目之一。

我怎么能做到这两件事?

试试这个方法:

  1. 在登录对话框的div中设置一个隐藏的输入。
  2. .dialog('open')之前先用problem_id设置
  3. 单击Login按钮回调成功后,从隐藏输入中检索problem_id并进行vote-up或vote-down操作。

希望有帮助

编辑:(试图在OP的第二个评论之后编写一个可行的示例)

<script type="text/javascript">
    $(document).ready(function() {
        var $dialog = $('#loginpopup')
                .dialog({
                    autoOpen: false,
                    title: 'Login Dialog'
                });
        var $problemId = $('#theProblemId', '#loginpopup');
        $("#newprofile").click(function () {
            $("#login_div").hide();
            $("#newprofileform").show();
        });

        $('.vote_up').click(function() {
            var problem_id = $(this).attr("data-problem_id");
            voteUp(problem_id);
            //Return false to prevent page navigation
            return false;
        });
        var voteUp = function(problem_id) {
            var dataString = 'problem_id=' + problem_id + '&vote=+';
            $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data) {
                    // ? :)
                    alert(data);
                },
                error : function(data) {
                    errorMessage = data.responseText;
                    if (errorMessage == "not_logged_in") {
                        //set the current problem id to the one within the dialog
                        $problemId.val(problem_id);
                        // Try to create the popup that asks user to log in.
                        $dialog.dialog('open');
                        // prevent the default action, e.g., following a link
                        return false;
                    }
                    else {
                        alert("not");
                    }
                    //alert(JSON.stringify(data));
                }
            });
        };
        $('.vote_down').click(function() {
            alert("down");
            problem_id = $(this).attr("data-problem_id");
            var dataString = 'problem_id=' + problem_id + '&vote=-';
            //Return false to prevent page navigation
            return false;
        });
        $('#loginButton', '#loginpopup').click(function() {
            $.ajax({
                url:'url to do the login',
                success:function() {
                    //now call cote up 
                    voteUp($problemId.val());
                }
            });
        });
    });
</script>

最新更新