簡體   English   中英

為什么我的 `if(result !== "false")` 返回 true?

[英]Why does my `if(result !== "false")` return true?

我進行了 ajax 調用來獲取數據。 如果它存在,例如結果設置我想運行另一個函數。

JS

success: function(result){ console.debug(result); console.log(result);
    if(result !== "false" || result !== ""){
        console.log('passed');    
    }

在控制台中,我除了“通過”。

結果為空,但我的 if 語句仍為真。 為什么?

這就是我發送的結果。 而且是空的...

PHP

$row = $stmt->fetch();
echo $row['objekt_nr'];

如果結果為"false"則它不是空字符串,並且 OR 的右側為真。

如果結果為""則它不是"false" ,OR 的左側為真。

如果是別的,那么 OR 語句的兩邊都是真的。

無論哪種方式,OR 語句都是正確的。

你想要一個 AND 而不是 OR。

條件result !== "false" || result !== "" result !== "false" || result !== ""將始終評估為真,因為 result 充其量只能是兩個值之一。 為了使條件為假, result必須同時為"""false"

例如:

  • 如果result === "false" then result !== ""必須為真(因為 result 是"false" ,而不是""

  • if result === "" then result !== "false"必須為真(因為結果是"" ,而不是"false"

  • 如果 result 既不是""也不是"false" ,則雙方都為真,評估為真。

你究竟想檢查什么?

if(result !== "false" && result !== "")如果 result既不是"false"也不是""則為真

if(result === "false" || result === "")如果結果為是真"false"""

你的 || 必須是&&。

if (result !== "false" || result !== "")語句永遠為真! 結果字符串永遠不能同時出現"false""" ……這是語句可以解析為false的唯一方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM