Javascript的奇异性——能够从脚本的一部分调用函数,但不能从另一部分调用

Javascript strangeness - Able to call a function from one part of the script, but not another

本文关键字:一部分 调用 函数 另一部 但不能 Javascript 脚本      更新时间:2023-09-26

我有一个JavaScript文件,其中有一个我引用的函数。当我从代码的一部分调用函数时,它可以工作,而从另一部分调用函数则不能。将函数代码放在调用的上面或下面似乎没有任何区别。

调用voteUp函数时发生错误。

下面是整个JS文件。你知道为什么对这个函数的两个不同的调用会有如此不同的结果吗?上面代码的调用给出了error: undefined function.

$(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();  
                    queue.login = true;
                    if (queue.voteUp) 
                    {
                        alert("in vote up queue: " + queue.voteUp + " and login: " + queue.login );
                        voteUp(queue.voteUp);
                    } 
                    // 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)
                {
                    queue.login = false;
                    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;
    });
});

$(document).ready(function() 
{
    queue = new Object; 
//  queue.login = false; 
     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");
        queue.voteUp = $(this).attr('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 );
        queue.voteUp = problem_id;
        var dataString = 'problem_id=' + problem_id + '&vote=+';
        alert ("login status: " + queue.login );
        if ( queue.login == false ) 
        {
            // Call the ajax to try to log in...or the dialog box to log in. requireLogin()
            $dialog.dialog('open');
            alert ("after dialog was open - in false case of being logged in.");
            // prevent the default action, e.g., following a link
            return false;           
        } 
        else 
        {
            // The person is actually logged in so lets have him vote
            $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data)
                {           
                    alert ("vote success, data: " + data);
                    // Try to update the vote count on the page
                    //$('p').each(function() 
                    //{ 
                        //on each paragraph in the page:
                      //  $(this).find('span').each() 
                      //  { 
                            //find each span within the paragraph being iterated over
                       // }
                     //}                      
                },
                error : function(data) 
                {
                    alert ("vote error");
                    errorMessage = data.responseText;
                    if ( errorMessage == "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.
                        $dialog.dialog('open');
                        // prevent the default action, e.g., following a link
                        return false;
                    }
                    else
                    {
                        alert ("not");
                    }    
                } // End of error case  
            }); // Closing AJAX call.
        } // Closing the else 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());
        //        }
        //    });  // Closing AJAX call.
    //});    
}); // End of document.ready() check

该函数被声明为第二个"ready"处理程序中的局部变量。该上下文之外的任何内容都不能调用它。

也许你可以让它成为一个全局函数,但这取决于"queue"是否真的应该是全局的。如果是,它真的应该显式地声明为全局(也就是说,在初始化代码之外使用var声明),因为您现在得到的内容无论如何都不能在"严格"模式下有效。