0

我一直在尝试让以下比较运算符工作。第二个“if”总是执行代码中的下一条语句。我想要的是能够检测到 entryType 或 followupEntryType 何时不等于字符串“Long Term Care” 这是代码......

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;
        }
    }
}
4

3 回答 3

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

如果要检查 EITHER 是否为假,则应使用&&, 并将代码移动到else块中

于 2013-03-16T19:22:54.900 回答
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;
  }
}
于 2013-03-16T19:23:12.950 回答
0

不,第二个if并不总是执行下一个语句。如果两个值都是"Long Term Care",那么它将执行else部分中的代码。IE:

<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"(即它将进入elseif any value is "Long Term Care"),您应该使用&&运算符:

if (!(document.getElementById('entryType').value == "Long Term Care") && !(document.getElementById('followupEntryType').value == "Long Term Care")) {
于 2013-03-16T19:31:21.050 回答