2013-03-16 42 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")) { 
相关问题