繁体   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