简体   繁体   English

为什么退出函数后布尔值会发生变化?

[英]Why does the value of a boolean change after stepping out of a function?

After I click on a li-element where the class-attribute contains "disabled selected", disabled gets the value "true" but after stepping out the function the last if-statement is called. 在单击class属性包含“ disabled selected”的li元素后,disabled将获得值“ true”,但在退出该函数后,将调用最后的if语句。

    var disabled = false;
    $("li").click(function () {
        if ($(this).attr("class") == "disabled selected") {
            disabled = true;
        }
    });
    if (disabled) {
        alert("disabled is true");
    }
    if (!disabled) {
        alert("disabled is false");
    }

You should move both the if statements inside the click handler. 您应该将两个if语句都移到点击处理程序中。

var disabled = false;
$("li").click(function () {
    if ($(this).attr("class") == "disabled selected") {
        disabled = true;
    }
    if (disabled) {
        alert("disabled is true");
    }
    else {
        alert("disabled is false");
    }
});

最后两个if测试在click事件处理程序之前运行,因此disabled的值尚未更改。

The callback is not invoked until the 'li' is clicked. 在单击“ li”之前,不会调用该回调。 However, the proceeding two 'if' statements are executed immediately. 但是,随后的两个“ if”语句将立即执行。 You'll want to place them within the callback. 您需要将它们放在回调中。

var disabled = false;
$("li").click(function () {
    if ($(this).attr("class") == "disabled selected") {
        disabled = true;
    }
    if (disabled) {
     alert("disabled is true");
    }
    if (!disabled) {
     alert("disabled is false");
    }
});

The function is executed when the li element is clicked. 单击li元素时将执行该功能。 When the script is initially evaluated, the click event has not been fired. 最初评估脚本时,尚未触发click事件。

The problem is two-fold. 问题是双重的。

First, as pointed out by several posters, you need to put those if tests inside the click handler. 首先,正如一些张贴者所指出的,您需要将这些if测试放入点击处理程序中。 As it stands now, they execute outside the click handler's scope, which I suspect is not when you want them to execute. 就目前而言,它们在点击处理程序范围之外执行,我怀疑不是在您希望它们执行时才执行。

Second, the if ($(this).attr("class") == "disabled selected") { will only get you a "truthy" value if the class string is 'disabled selected' . 其次, if ($(this).attr("class") == "disabled selected") {仅当类字符串为'disabled selected'您才能得到一个“真实的”值。 If the class string is 'selected disabled' (or 'otherClass disabled selected' or 'disabled selected draggable' , etc.) then you will get a false value returned as a result of the comparison. 如果类字符串为'selected disabled' (或'otherClass disabled selected''disabled selected draggable'等),则比较结果将返回false值。

Try: 尝试:

$("li").click(function () {
    var i = $(this); //Multiple variables used for better step-line debugging.
    var hasDisabled = i.hasClass('disabled');
    var hasSelected = i.hasClass('selected');
    var disabled = false;
    if (hasDisabled && hasSelected) {
        disabled = true;
    }
    if (disabled) {
        alert("disabled is true");
    }
    if (!disabled) {
        alert("disabled is false");
    }
});

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

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