简体   繁体   English

从另一个函数获取javascript变量值

[英]Get javascript variable value from another function

I've declared a variable in the first .click function, but on my second .click I am trying to get the first variable value, but it's not been able to find it, due to it being in a different function. 我宣布第一。点击功能的变量,但我的第二个。点击我想获得的第一个变量的值,但它没能找到它,因为它在不同的功能之中。

How can I set a global variable or something where other functions can use the value too? 如何设置全局变量或其他函数也可以使用该值的东西?

$('input[type="checkbox"]').click(function(){
    var checked = 0;
    if($(this).prop("checked") == true){
        var checked = 1
        console.log('checked');
        console.log(checked);
    }
    else if($(this).prop("checked") == false){
        var checked = 0
        console.log('not checked');
        console.log(checked);
    }
});
$('#button-cart').click(function(){
    if(checked == 1){
        alert('can continue fine');
    } else if(checked == 0){
        alert('cannot continue');
    }
})

Error: Uncaught ReferenceError: checked is not defined 错误: 未捕获ReferenceError:未定义

To avoid polluting global scope, the best practice would be to declare the checked variable within a closure so that only your event handlers can access it. 为了避免污染全局范围,最佳实践是在闭包中声明检查的变量,以便只有事件处理程序才能访问它。

(function() {
    var checked; // No one outside of this immediately invoked function can see the checked variable.
    $('input[type="checkbox"]').click(function(){
        checked = 0;
        if($(this).prop("checked") == true){
            checked = 1
            console.log('checked');
            console.log(checked);
        }
        else if($(this).prop("checked") == false){
            checked = 0
            console.log('not checked');
            console.log(checked);
        }
    });
    $('#button-cart').click(function(){
        if(checked == 1){
            alert('can continue fine');
        } else if(checked == 0){
            alert('cannot continue');
        }
    })
}());

This way, both your click handlers can see the checked variable but it can't be accessed by anyone else. 这样,您的两个点击处理程序都可以看到checked变量,但其他任何人都无法访问它。

Declare checked outside the functions - this makes it accessible on the global scope. 在函数外部声明checked -这使其可以在全局范围内访问。 Then remove the var statements inside the function, or you will create a new variable in the function scope. 然后删除函数内的var语句,否则您将在函数范围内创建一个新变量。 This code will work: 该代码将起作用:

var checked = 0;
$('input[type="checkbox"]').click(function(){
    if($(this).prop("checked") == true){
        checked = 1
        console.log('checked');
        console.log(checked);
    }
    else if($(this).prop("checked") == false){
        checked = 0
        console.log('not checked');
        console.log(checked);
    }
});
$('#button-cart').click(function(){
    if(checked == 1){
        alert('can continue fine');
    } else if(checked == 0){
        alert('cannot continue');
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM