$('#firstname').bind('focusout focusin', function() {
var get_firstname = $("#firstname").val().replace(/ /g, "+");
var n_firstname = get_firstname.length;
if (n_firstname > 2) {
$(".firstname_good").fadeIn("fast");
var firstname_v = 1;
}
else {
$(".firstname_good").fadeOut("fast");
var firstname_v = '';
}
});
我无法理解如何使用下面的 VAR firstname_v。在上面我需要做什么才能在下面使用它
$('#lastname').bind('focusout focusin', function() {
if (firstname_v == 1) {
alert(firstname_v);
}
});
最佳答案
您需要在两个内部作用域都可以访问的更高作用域中声明变量:
var firstname_v; // declare it
$('#firstname').bind('focusout focusin', function() {
var get_firstname = $("#firstname").val().replace(/ /g, "+");
var n_firstname = get_firstname.length;
if (n_firstname > 2) {
$(".firstname_good").fadeIn("fast");
firstname_v = 1; // no var here, you're just setting it
}
else {
$(".firstname_good").fadeOut("fast");
firstname_v = ''; // no var here, you're just setting it
}
});
$('#lastname').bind('focusout focusin', function() {
if (firstname_v == 1) { // no var here, you're just getting it
alert(firstname_v);
}
});
关于javascript - 在 Jquery 或 Javascript 中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476494/