我正在尝试制作一个密码检查系统。 我决定还想检查一下它是否是常用密码。
为此,我将外部 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 变量,这可以解决此问题吗?
最佳答案
将 commonPass
移出所有函数的范围
jQuery(document).ready(function() {
var commonPass = new Array;
...应该是...
var commonPass = new Array;
jQuery(document).ready(function() {
或者您可以使用window.commonPass
。 window
是 JavaScript 在浏览器中工作时的“全局”变量。 Javascript 的变量存在于它们所声明的函数的作用域内(如果在函数外部,则存在于 window
范围内)。
关于javascript - 为另一个函数保留 JQuery 变量(全局变量?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29041216/