简体   繁体   English

如果条件执行,为什么不使用简单的Javascript?

[英]Why won't a simple Javascript if condition execute?

<!DOCTYPE html>
<html>
<body>

<button onClick="Display()"></button>

<script>
function Display() {
    var a = 0;
    if(a = 0){
      alert("hello world");
    }
} 

</script>

</body>
</html>

I run the page and clicked the button and nothing happens. 我运行页面并单击按钮,但没有任何反应。 I can't for the life of me figure out why... 我无法为自己的生命找出原因...

Edit: on another note, THIS code executes no matter what, even if I haven't defined var CorrectActivities: 编辑:另一方面,即使我没有定义var CorrectActivities,此代码也无论执行什么:

function Display() {

    if(CorrectActivities = 3) {
        document.getElementById("ActivitiesResult").innerHTML = '<span>style="color:DarkGreen;font-weight:bold;font-size:30px;">3 out of 3 correct!</span>';

    } else if (CorrectActivities = 2) {
        document.getElementById("ActivitiesResult").innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">2 out of 3 correct!</span>';

    } else if (CorrectActivities = 1) {
        document.getElementById("ActivitiesResult").innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">1 out of 3 correct!</span>';

    } else {
    document.getElementById("ActivitiesResult").innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">0 out of 3 correct!</span>';
    }
}

Edit2: thanks for the answers. Edit2:感谢您的答案。 First code is fixed, second is still broken regardless of what I try. 无论我尝试什么,第一个代码都是固定的,第二个代码仍然是坏的。 Going to look for errors elsewhere in the script... 要在脚本中的其他位置查找错误...

When you compare values, you should use == (equal) or === (equal and same type). 比较值时,应使用== (等于)或=== (等于和相同类型)。 = is only for setting variable value. =仅用于设置变量值。

 <!DOCTYPE html> <html> <body> <button onClick="Display()"></button> <script> function Display() { var a = 0; if(a == 0){ alert("hello world"); } } </script> </body> </html> 

To your another code, I created a jsfiddle . 对于您的另一个代码,我创建了一个jsfiddle There you have same problem as in first part, wrong comparison operator. 与第一部分中的问题相同,比较运算符错误。 It didn't run if CorrectActivities wasn't declared. 如果没有声明CorrectActivities ,它将无法运行。 I fixed it (like in first part), declared variable and added three buttons to test all cases and it seems to work. 我修复了它(就像在第一部分中一样),声明了变量,并添加了三个按钮来测试所有情况,它似乎可以工作。

You also have small typo in case 3, you have an extra closing bracket at <span>style 在第3种情况下,您也有小的错别字,在<span>style有一个额外的结束括号

a = 0 is assignment not a valid condition. a = 0表示分配不是有效条件。 a == 0 , a === 0 or !a will all work for what you want. a == 0a === 0!aa === 0您的需求。

Same is the case with CorrectActivities , use CorrectActivities == 3 instead of CorrectActivities = 3 . CorrectActivities相同,请使用CorrectActivities == 3而不是CorrectActivities = 3

in javascript = assign a value and return it, == or === compare values 在javascript中=分配一个值并返回它, =====比较值

The first part wont execute because: 第一部分不会执行,因为:

  • You click the button 您单击按钮
  • Display get called Display被叫
  • if(a = 0) evaluate to if(0) which is to javascript if(false) if(a = 0)评估为if(0) ,即javascript if(false)

so the code inside if won't run. 所以里面的代码if将不会运行。 what you want is if(a == 0) : 你想要的是if(a == 0)

 function Display() { var a = 0; if (a == 0) { alert("hello world"); } } 
 <button onClick="Display()"></button> 

for the second part if(CorrectActivities = 3) will be if(3) which is to javascript if(true) so it will always run. 对于第二部分, if(CorrectActivities = 3)将是if(3) ,这将是javascript if(true)因此它将始终运行。 again what you want is using the == operator: 再次,您想要使用==运算符:

function Display () {
  if ((CorrectActivities == 3)) {
    document.getElementById('ActivitiesResult').innerHTML = '<span>style="color:DarkGreen;font-weight:bold;font-size:30px;">3 out of 3 correct!</span>'
  } else if ((CorrectActivities == 2)) {
    document.getElementById('ActivitiesResult').innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">2 out of 3 correct!</span>'
  } else if ((CorrectActivities == 1)) {
    document.getElementById('ActivitiesResult').innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">1 out of 3 correct!</span>'
  } else {
    document.getElementById('ActivitiesResult').innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">0 out of 3 correct!</span>'
  }
}

This is because instead of comparing,you are assigning it. 这是因为没有分配比较,而是分配了它。

So the statement if(a = 0) always comes out to be true. 因此,语句if(a = 0)总是正确的。

Use == instead of =. 使用==代替=。

function Display() {
    var a = 0;
    if(a = 0){
        alert("hello world");
    }
} 

This will help. 这会有所帮助。

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

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