Javascript 为另一个函数保留JQuery变量(全局变量?)

Javascript 为另一个函数保留JQuery变量(全局变量?),javascript,jquery,arrays,passwords,Javascript,Jquery,Arrays,Passwords,我想做一个密码检查系统。 我决定还要检查它是否是常用密码 为此,我将一个外部txt文件加载到一个数组中。但是,我的密码检查功能似乎无法读取此数组 jQuery(document).ready(function() { var commonPass = new Array; jQuery.get('/static/commonPass.txt', function(data){ commonPass = data.split('\n'); console.log(commonPas

我想做一个密码检查系统。 我决定还要检查它是否是常用密码

为此,我将一个外部txt文件加载到一个数组中。但是,我的密码检查功能似乎无法读取此数组

jQuery(document).ready(function() {
var commonPass = new Array;
jQuery.get('/static/commonPass.txt', function(data){
    commonPass = data.split('\n');
    console.log(commonPass);
});
console.log(commonPass);
//you have to use keyup, because keydown will not catch the currently entered value
jQuery('input[type=password]').keyup(function() { 

    // set password variable
    var pswd = jQuery(this).val();

    //check if common password
    console.log(pswd);
    if ( jQuery.inArray(str.toLowerCase(pswd), commonPass)!= -1) {
        console.log('InArray');
        jQuery('#known').removeClass('valid').addClass('invalid');
    } else {
        console.log('NotInArray');
        jQuery('#known').removeClass('invalid').addClass('valid');
    }

});

是否可以使全局jQuery/Javascript变量成为这个问题的解决方案?

Move
commonPass
超出所有函数的范围

jQuery(document).ready(function() {
var commonPass = new Array;
…应该是

var commonPass = new Array;
jQuery(document).ready(function() {

或者您可以使用
window.commonPass
<代码>窗口是javascript在浏览器中工作时的“全局”变量。Javascript的变量存在于它们在其中声明的函数的作用域内(或者
window
如果在函数之外)

当你试图理解数组时,你收到了什么错误?数组只是空的,你已经回答了。它正在工作。它的加载非常好,只是不知道如何在加载函数的范围之外访问var。