簡體   English   中英

JavaScript不是(a == b)|| 不(c == b)

[英]Javascript Not (a==b) || Not (c==b)

我一直在嘗試讓以下比較運算符起作用。 第二個“ if”總是執行代碼中的下一條語句。 我想要的是能夠檢測到entryType或followupEntryType不等於字符串“長期護理”的情況。這是代碼...

function getComboB(sel) {

    var value = sel.options[sel.selectedIndex].value;
    if (value == "Rehab") {
        if (!(document.getElementById('entryType').value == "Long Term Care") || !(document.getElementById('followupEntryType').value == "Long Term Care")) {
            document.getElementById('followupEntryType').value = "Long Term Care";
            alert("short-term rehab code");
            return true;
        } else {
            alert("long-term rehab code");
            return true;
        }
    }
}

這是怎么回事,這就是您想要的嗎?

if(true || true)
{
console.log('second check will not be executed');
}

if(true || false)
{
console.log('second check will not be executed');
}

if(false || false)
{
console.log('second check will be executed');
}

if(false || true)
{
console.log('second check will be executed');
}

如果要檢查EITHER是否為false,則應使用&&並將代碼移到else塊中

function getComboB(sel) {

var value = sel.options[sel.selectedIndex].value; 
if (value == "Rehab") {
  if (document.getElementById('entryType').value != "Long Term Care" && document.getElementById('followupEntryType').value != "Long Term Care") {
    document.getElementById('followupEntryType').value = "Long Term Care";
    alert ("short-term rehab code");
    return true;
  } else {
    alert ("long-term rehab code");
    return true;
  }
}

不,第二個if並不總是執行下一條語句。 如果兩個值均為"Long Term Care" ,則它將在else部分中執行代碼。 即:

<input type="text" id="entryType" value="Long Term Care" />
<input type="text" id="followupEntryType" value="Long Term Care" />

演示: http//jsfiddle.net/QW43B/

如果你想的條件是真實的,如果雙方的值不同"Long Term Care" (即它會進入else只要有一個是"Long Term Care" ),你應該使用&&操盤手:

if (!(document.getElementById('entryType').value == "Long Term Care") && !(document.getElementById('followupEntryType').value == "Long Term Care")) {

暫無
暫無

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

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