简体   繁体   English

else 语句不起作用

[英]Else statement does not work

I don't know why my else statement didn't work.我不知道为什么我的 else 语句不起作用。

It's my first time that actually happened and I checked using console.log() .这是我第一次真正发生,我使用console.log()检查。

This is my code:这是我的代码:

setTimeout(function() {
  var b = 2;
  var d = 80000;
  if ("div[class='resultInfo resultLose']") {
    setTimeout(function() {
      var a = ($("input[name='_4']").val().replace(/\s+/g, ''));
      var c = a * b;
      $("input[name='_4']").val(c);
      $("input[value='Play!']").click();
    }, 1000);
  } else if ("div[class='resultInfo resultWin']") {
    setTimeout(function() {
      $("input[name='_4']").val(d);
      $("input[value='Play!']").click();
    }, 1000);
  }
}, 4500);

I have tried to:我试图:

  1. delete the setTimeout in ifif删除setTimeout
  2. delete the " if ("div[class='resultInfo resultWin']") "删除“ if ("div[class='resultInfo resultWin']")

This only uses first if even the elseif is true , I don't know what to do.这仅使用第一ifelseiftrue ,我不知道该怎么办。

You are having a non-empty string inside your if condition, which evaluates to true every time.您的 if 条件中有一个非空字符串,每次都评估为真。 So else block will never be reached.所以永远不会到达 else 块。

Your current if and else/if conditions are just testing the truthiness of a random string which will always evaluate to true.您当前的 if 和 else/if 条件只是测试随机字符串的真实性,该字符串将始终评估为真。 Since the first condition evaluates to true, you will always skip the else if condition (even though that will also evaluate to true in this instance).由于第一个条件的计算结果为真,您将始终跳过 else if 条件(即使在这种情况下也将评估为真)。

What you actually seem to want to test is whether or not there are elements that exist that match the strings you have as selector strings .您实际上似乎想要测试的是,是否存在与您作为选择器字符串匹配的字符串的元素。 If that's the case, your simplest fix would be to change your if and else statements to look more like jQuery statements and checking the length of the result.如果是这种情况,最简单的解决方法是将 if 和 else 语句更改为看起来更像 jQuery 语句并检查结果的长度。

if ($("div[class='resultInfo resultLose']").length > 0)

and

else if ($("div[class='resultInfo resultWin']").length > 0)

Your code doesn't make sense.你的代码没有意义。

You aren't entering the else if because your if will always evaluate to true .您没有输入else if因为您的if将始终评估为true

if ("div[class='resultInfo resultLose']") will always be true , as you're not doing any comparision, you're evaluating a string. if ("div[class='resultInfo resultLose']")将始终为true ,因为您没有进行任何比较,您正在评估一个字符串。 So that's the same as doing if (true) .所以这与执行if (true)

I supose you're missing some code there, like a JQuery selection or something like that.我想你在那里遗漏了一些代码,比如 JQuery 选择或类似的东西。

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

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