如何通过各种调用保留特定的项id



我正在尝试这样做:

让用户点击一些动作。该操作检查用户是否登录。如果不正确,则打开登录对话框,如果用户登录正确,则更新原页面。

我得到了几乎所有这些工作,除了成功登录后的部分。问题是代码似乎无法访问我试图更新的item_id。相反,当我尝试在弹出框的登录表单中设置项目id(在本例中为problem_id)时,id是页面上最后一个id的编号,而不是被单击的id的编号。

下面是我试图在jQuery中做的事情:

<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();
        });
    // Called right away after someone clicks on the vote up link
    $('.vote_up').click(function() 
    {        
        var problem_id = $(this).attr("data-problem_id");
        //alert ("In vote up click, problem_id: " + problem_id );
        voteUp(problem_id);
        //Return false to prevent page navigation
        return false;       
    });
    var voteUp = function(problem_id) 
    {
        alert ("In vote up function, problem_id: " + problem_id );
        var dataString = 'problem_id=' + problem_id + '&vote=+';
        $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data)
                {           
                    alert ("vote success, data: " + data);
                    // ? :)
                },
                error : function(data) 
                {
                    alert ("vote error");
                    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');
                        alert ("after dialog was open");
                        // prevent the default action, e.g., following a link
                        return false;
                    }
                    else
                    {
                        alert ("not");
                    }
                    //alert(JSON.stringify(data));
                } // Closing error case
            }); // Closing AJAX call.
    };
    $('.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() 
    {
    alert("in login button fnction");
            $.ajax({
                url:'url to do the login',
                success:function() {
                    //now call cote up 
                    voteUp($problemId.val());
                }
            });
        });    
});
</script>

,下面是登录表单:

         <div id="login_div">
         <form id="login_form" method="post" action="">
         <p>
             <label for="name"><span>Your Email:</span></label> <input type="text" name="email" id="email" />
         </p>
         <p>
             <label for="name"><span>Your Password:</span></label> <input type="password" name="user_pass" id="user_pass">
         </p>
         <input type="hidden" id="problem_id" name="problem_id" value="<?php echo $problem_id; ?>" />
<span class="no_such_user" style="color: red; display:none">The login and password does not match our records.</span>
<span class="password_error" style="color: red; display:none">The password much be 5 characters or more.</span>
<span class="login_success" style="color: green; display:none">You successfully logged in.</span>
         <p>
            <input type="submit" value="Log In"  />
         </p>
         </form>
         </div>

但是在这个表单中设置的问题_id不是被点击的那个,即使我试图将它保存在我的jQuery中

同样,下面是登录时执行的代码:

$(function()
{
    $("#login_div input[type=submit]").click(function()
    {
        var email = $("#email").val();
        var password = $("#user_pass").val();
        //alert("Email: " + email);
        //alert("password: " + password);
        var dataString = 'email='+ email + '&password=' + password;
        if( !email )
        {   
            alert ("1");
            $('.login_success_email_empty').fadeOut(200).hide();
            $('.login_error_email_empty').fadeOut(200).show();
        }       
        else        
        if( !password || password.length < 5)
        {alert ("2");
            $('.password_success').fadeOut(200).hide();
            $('.password_error').fadeOut(200).show();
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "../auth/login_ajax.php",
                dataType: "json",
                data: dataString,
                success: function(json)
                {
                    $('.password_error').fadeOut(200).hide();
                    $('.no_such_user').fadeOut(200).hide(); 
                    $('.login_success_email_empty').fadeOut(200).hide();
                    $('.login_success').fadeIn(200).show();                                 
                    // Closing the dialog bosx
                    $('#loginpopup').dialog('close');
                    // Swapping out the header div
                    $('#header_logged_out').hide();
                    $('#header_logged_in').show();  
                    // Now also need to retrieve the problem_id                 
                    problem_id = $("#problem_id").val();
                    //$problemId = $('#theProblemId', '#loginpopup').val();         

                    var $problemId = $('#theProblemId', '#loginpopup');
                    alert ("After login, problem_id: " + problem_id + " and problemId was: " + $problemId);
                },
                error : function(json)
                {
                    alert ("error");
                    // Output the result.
                    errorMessage = json.responseText;
                    alert ("ErrorMessage: " + errorMessage );
                    if ( errorMessage == 'no_such_user' )
                    {
                        $('.no_such_user').fadeOut(200).hide();
                        $('.no_such_user').fadeOut(200).show();                 
                    }
                }
            });
        }
        return false;
    });
});

和我只是不知道如何得到在原来的jQuery代码中设置的问题_id在登录后执行的jQuery代码中被识别

因为我真正需要做的是根据用户是否登录来更新那个特定的问题

在处理投票的原始代码中,我如何知道对话框中的登录是否成功?

顺便说一下,我正在做这个页面:http://www.problemio.com

似乎你可能会更好地使用jquery表单/ajax提交插件:http://be.twixt.us/jquery/formSubmission.php

$('loginDiv > form').submit(function() {
    var options = {
        url = "../auth/login_ajax.php",
        [...]
       };
    $(this).ajaxSubmit(options);
    return false;  //this line is required to make sure that the page doesn't get redirection to /auth/login_ajax.php
});
这样它就不会重新加载任何javascript,保留任何已经设置的变量。因此,您可以在登录前设置问题ID,然后在登录后使用它进行操作。

最新更新