[英]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.