2013-03-16 120 views
0

我一直試圖讓下面的比較運算符工作。第二個'if'總是執行代碼中的下一個語句。我要的是能夠檢測到何時eith entryType或followupEntryType不等於字符串「長期護理保險」這裏是代碼...Javascript不是(a == b)||不(c == b)

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; 
     } 
    } 
} 
+0

你的意思是'&&'而不是'||'? – 2013-03-16 19:20:19

+3

順便說一下,不縮進是花費太多時間試圖查看代碼無法工作的一種肯定方式。 – 2013-03-16 19:20:41

+0

用[this]重新格式化你的代碼(http://jsbeautifier.org/) – 2013-03-16 19:21:23

回答

0
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; 
    } 
} 
+0

在審查了函數getComboB(sel)之前的代碼之後,我意識到我有一個邏輯錯誤。但是,如果聲明是這樣的,並且在代碼中發生更改(如果有效),我的確改變了。我感謝所有爲這個問題提供建議的人。他們都提供了良好的視覺。 – user2033850 2013-03-18 19:47:28

+0

我對這個建議投了贊成票。 – user2033850 2013-03-18 19:52:27

1

這裏是怎麼回事,是你想要的嗎?

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'); 
} 

如果你想檢查是否無論是是假的,你應該使用&&,並在else塊移動你的代碼

+0

謝謝,我會嘗試您的建議。我認爲,我可以簡單地通過以下代碼編寫代碼: – user2033850 2013-03-16 19:41:39

+0

對不起,但我將不得不在後面跟進。 – user2033850 2013-03-16 19:45:03

0

沒有,第二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")) { 
相關問題