简体   繁体   English

如果checbox / disable元素无法正常工作

[英]If checbox/disable element not working properly

I'm attempting to make a script so that if you check/uncheck a checkbox input, that it will enable/disable a target input text box. 我试图制作一个脚本,以便如果您选中/取消选中复选框输入,它将启用/禁用目标输入文本框。 My code if statement doesn't seem to function correctly sometimes firing both ifs at once, other times not firing at all. 我的代码if语句似乎无法正确运行,有时会一次触发两个if,而有时根本不会触发。 I've done debugging and tried numerous variations but it still won't work. 我已经完成调试并尝试了多种变体,但仍然无法正常工作。 Here is what I have, any help is greatly appreciated! 这是我所拥有的,非常感谢您的帮助!

function disable(elem) {
    var obj = document.getElementById(elem);
    status = obj.disabled;
    console.log(status);
    if (status = true) {
        console.log("test");
        obj.disabled = false;
        obj.style.backgroundColor = "white";
    }
    if (status = false) {
        console.log("test2");
        obj.disabled = true;
        obj.style.backgroundColor = "bfbfbf";
    }
}

This code is not exhibiting proper behavior for several reasons. 由于多种原因,此代码未表现出适当的行为。

The first reason is that your if statements are inline, not in an if-else form. 第一个原因是您的if语句是内联的,而不是if-else形式。 A better organization is as follows: 更好的组织如下:

function disable(elem) {
    var obj = document.getElementById(elem);
    status = obj.disabled;
    console.log(status);
    if (status == true) {
        console.log("test");
        obj.disabled = false;
        obj.style.backgroundColor = "white";
    } else {
        console.log("test2");
        obj.disabled = true;
        obj.style.backgroundColor = "#bfbfbf";
    }
}

This means that even if the variable you are checking changes while the code in the block is executing, it will not execute the code in the opposing block regardless of its new value. 这意味着即使您正在检查的变量在执行块中的代码时发生更改,无论其新值如何,它都不会执行相反块中的代码。 As you have it, if the value were to change while the code in the first if statement was running, then it is possible for both control blocks to run. 如您所料,如果在运行第一个if语句中的代码时更改了值,则两个控制块都可能运行。

The second reason it is not behaving right is due to the incorrect syntax within your if statement. 行为不正确的第二个原因是由于if语句中的语法不正确。 You are currently using the = operator which means set the variable to what you are wanting to check against. 您当前正在使用=运算符,这意味着将变量设置为要检查的变量。 You must use either the == or === equality checks (the latter is type strict) if you want to write them this way. 如果要以这种方式编写它们,则必须使用=====等式检查(后者是类型严格的)。 An even better way is to omit that operator entirely by just checking if the value is truthy, like so: 更好的方法是仅通过检查值是否为真来完全忽略该运算符,如下所示:

function disable(elem) {
    var obj = document.getElementById(elem);
    status = obj.disabled;
    console.log(status);
    if (status) {
        console.log("test");
        obj.disabled = false;
        obj.style.backgroundColor = "white";
    } else {
        console.log("test2");
        obj.disabled = true;
        obj.style.backgroundColor = "#bfbfbf";
    }
}

This should give you your expected control behavior :) As was mentioned, make sure you are formatting your values appropriately (eg "#bfbfbf" not "bfbfbf") 这应该给您预期的控制行为:)如前所述,请确保正确设置值的格式(例如,“#bfbfbf”而非“ bfbfbf”)

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

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