简体   繁体   English

如何在函数中操作布尔值?

[英]How do I manipulate a boolean in a function?

I'm trying to change inputX[0] from false to true , then get an alert if it worked.我正在尝试将inputX[0]false更改为true ,然后在它有效时收到警报。 Unfortunately I don't get the message that inputX[0] was set to true .不幸的是,我没有收到inputX[0]设置为true的消息。 Do you have any ideas?你有什么想法?

 <body> <div> <button id="S1" onclick="btnManager(this);"></button> </div> <script> var inputX = new Array(); definedInputs(); btnManager(); question(); function definedInputs() { inputX[0] = false; } function btnManager(pressedBtn) { if (pressedBtn.id == (id = "S1")) { inputX[0] = true; } } function question() { if (inputX[0] == true) { alert("inputX is set to true"); } } </script> </body>

try with this:试试这个:

<body>
  <div>
    <button id="S1" onclick="btnManager(this);"></button>
  </div>

  <script>
    var inputX = new Array();
    inputX[0] = false;

    function btnManager(pressedBtn) {
        inputX[0] = !inputX[0];
        alert(inputX[0]);
    }
  </script>
</body>

With this, every you push the button the value will be set by the negation of the current value, hope it helps.有了这个,每次你按下按钮,值都会被当前值的否定设置,希望它有帮助。

It is important that you understand the Js lifecycle.了解 Js 生命周期很重要。 First javascript objects and functions are built and then the code is executed, in this case it happens as follows:首先构建 javascript 对象和函数,然后执行代码,在这种情况下,它发生如下:

  1. The array "inputX" is created数组“inputX”被创建
  2. Using the function "definedInputs()" defines "inputX[0] = false"使用函数“definedInputs()”定义了“inputX[0] = false”
  3. "btnManager()" is executed but since it is not assigned a parameter, the value of "pressedBtn.id" is "undefined" so the state of "inputX[0]" does not change “btnManager()”被执行,但由于它没有分配参数,“pressedBtn.id”的值是“undefined”,所以“inputX[0]”的状态不会改变
  4. The status of "inputX[0]" is queried using "question()", but since "inputX[0]" is still false, no alert is fired.使用“question()”查询“inputX[0]”的状态,但由于“inputX[0]”仍然为假,因此不会触发任何警报。

All of this happens before you can press the button.所有这一切都发生在您按下按钮之前。 Pressing the button executes "btnManager(this)" and since the id is equal to "S1" the state of "inputX[0]" changes to true.按下按钮执行“btnManager(this)”,由于 id 等于“S1”,“inputX[0]”的状态变为 true。

But you can't see this change because the "question()" function has already been executed.但是你看不到这个变化,因为“question()”函数已经被执行了。

Try:尝试:

<!DOCTYPE html>
<html>
<body>
  <div>
    <button id="S1" onclick="btnManager(this);">Test</button>
  </div>

  <script>
    var inputX = new Array();
    definedInputs();

    function definedInputs() {
      inputX[0] = false;
    }

    function btnManager(pressedBtn) {
      if (pressedBtn.id == "S1") {
        inputX[0] = true;
      }
      question();
    }

    function question() {
      if (inputX[0] == true) {
        alert("inputX is set to true");
      }
    }
  </script>
</body>
</html>

Add some console logs to see what is happening.添加一些控制台日志以查看发生了什么。 If you do, you'll see that all of your functions are executing immediately.如果你这样做了,你会看到你的所有函数都在立即执行。 When you click the button and enter the btnManager() function what happens next?当您单击按钮并进入btnManager()函数时,接下来会发生什么? As you'll see, your code is executing the btnManager() function but then what about checking for your alert?正如您将看到的,您的代码正在执行btnManager()函数,但是如何检查您的警报呢?

If you call question() after your if statement, then you'll run your check again.如果您在 if 语句之后调用question() ,那么您将再次运行检查。

You could do this with less lines, but for the sake of keeping your exact code and making it work, this is how you would achieve your goal:您可以用更少的行数来做到这一点,但为了保持您的准确代码并使其工作,这就是您实现目标的方式:

<body>
  <div>
    <button id="S1" onclick="btnManager(this);"></button>
  </div>

  <script>
    var inputX = new Array();
    // You could really remove these and do it like Lucretius's Answer
    definedInputs(); 
    btnManager();
    question();

    function definedInputs() {
      inputX[0] = false;
    }

    function btnManager(pressedBtn) {
      if (pressedBtn.id == (id = "S1")) {
        inputX[0] = true;
      }

      question();
    }

    function question() {
      if (inputX[0] == true) {
        alert("inputX is set to true");
      }
    }
  </script>
</body>

You're comparing the value of pressedBtn.id which is a text string S1 to a true/false Boolean evaluation (id == "S1") which will always be false because text strings and boolean values are not the same.您正在将作为文本字符串S1pressedBtn.id的值与 true/false 布尔评估(id == "S1")进行比较,因为文本字符串和布尔值不相同,所以该值始终为 false。 Since (id = "S1") is an assignment, and you can't compare an assignment, this is what I am guessing you're trying to do.由于(id = "S1")是一个作业,你不能比较一个作业,这就是我猜你正在尝试做的事情。

  • 'S1' == true will always be false 'S1' == true永远是 false
  • 'S1' == false will also always be false. 'S1' == false也将始终为 false。

Instead of:代替:

    function btnManager(pressedBtn) {
      if (pressedBtn.id == (id == "S1")) {
        inputX[0] = true;
      }
    }

Just evaluate the id and then log it to console to make sure the input array is updated.只需评估 id,然后将其记录到控制台以确保输入数组已更新。

    function btnManager(pressedBtn) {
      if (pressedBtn.id === "S1" ) {     //true when button id = "S1"
        inputX[0] = true;
        console.log( inputX );           //expected array output "[true]"
        
        console.log( ('S1' == false) );  //always false
        console.log( ('S1' == true) );   //always false
      }
    }

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

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