即使我返回false,jQuery也会在url的末尾添加#;

时间:2011-12-24 14:43:06

标签: javascript jquery

我有这个代码,我贴上了返回false;这样它在执行后不会在URL的末尾附加#,但它仍然会这样做。知道我做错了什么以及如何/哪里最好返回假?

    // Called right away after someone clicks on the vote up link
    $('.vote_up').mouseup(function() 
    {        
        $("#loading").show();
        var problem_id = $(this).attr("data-problem_id");

        vote(problem_id , 1);

        //Return false to prevent page navigation
        return false;       
    });

    $('.vote_down').mouseup(function() 
    {
        $("#loading").show();
        problem_id = $(this).attr("data-problem_id");

        vote ( problem_id , -1 );

        //Return false to prevent page navigation
        return false;
    });       

// Global function
var vote = function(problem_id , vote) 
{
    var dataString = 'problem_id=' + problem_id + '&vote=' + vote;

    // The person is actually logged in so lets have him vote
    $.ajax({
        type: "POST",
        url: "/auth/check_login.php",
        dataType: "json",
        success: function(data)
        {
            $.ajax({    
                type: "POST",
                url: "/problems/vote.php",
                data: dataString,
                success: function(html)
                      { 
                           $("#loading").hide();
                           if ( html == "not_logged_in" )
                           {
                               queue.login = false;

                               //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.
                               $("#loginpopup").dialog(  {title: 'Please Login To Vote'} );  // title: 'Login Dialog'

                               // prevent the default action, e.g., following a link
                               return false;
                           }
                           else
                           if ( html == "already_voted" )
                           {
                               // Display a dialog box saying that the user already voted
                               $('<div>You already voted this way on this problem.</div>').dialog( {title: 'Already Voted'});
                               // show div which says un-important, hide div which says important

                               $("#support").hide();
                               $("#dont_support").show(); 

                               return false;                              
                           }
                           else
                           if (  html == "error_getting_vote" )
                           {
                               $('<div />').html('Error getting existing votes.').dialog();
                           }
                           else
                           {
                               if ( vote == -1 )
                               {
                                   $("#support").show();
                                   $("#dont_support").hide();                                                              
                               }
                               else
                               {
                                   $("#support").hide();
                                   $("#dont_support").show();                                                              
                               }

                               // Now make a call to AJAX to get the count of votes
                               $.ajax({    
                                   type: "POST",
                                   url: "/problems/get_vote_count.php",
                                   data: dataString,
                                   success: function(html)   
                                   {
                                       var class_name = ".votes_"+problem_id;

                                       $(class_name).text(html);

                                       return false;                                   
                                   }
                                });    

                                return false;                            
                           }                        
                      },
                      error: function(html) 
                      {
                          $("#loading").hide();

                          return false;
                      } // End of error case    
                }); // Closing inner AJAX call.
            },
            error: function(data)
            {
                $("#loading").hide();
                $("#loginpopup").dialog( {title: 'Please Login To Vote'} );

                return false;
            } // End of error
        }); // End of outer AJAX.

        return false;
};    

2 个答案:

答案 0 :(得分:6)

您应该将事件侦听器绑定到click事件而不是mouseup。触发mouseup时,默认行为已经发生,无法再取消。

$('.vote_down').click(function(e) {
    e.preventDefault();
    // Rest of code

PS。根据您的代码判断,您有单独的页面投票:

  • AJAX请求 - 已登录?
  • AJAX请求 - 投票

如果投票页面不包含某种身份验证,则此模型非常不安全。用户可以通过创建对投票页面的人为请求来轻松绕过登录页面。

您应该合并登录页面和投票页面。

答案 1 :(得分:1)

将链接语法从<a href="#">更改为<a href="javascript:void(0);" />,而不更改您的js代码。