Javascript Not(a==b)| Not(c==b)
我一直在尝试让下面的比较操作符工作。第二个“if”总是执行代码中的下一条语句。我想要的是能够检测entryType或followupEntryType何时不等于字符串“Long Term Care”,这里是代码Javascript Not(a==b)| Not(c==b),javascript,comparison,logical-operators,Javascript,Comparison,Logical Operators,我一直在尝试让下面的比较操作符工作。第二个“if”总是执行代码中的下一条语句。我想要的是能够检测entryType或followupEntryType何时不等于字符串“Long Term Care”,这里是代码 function getComboB(sel) { var value = sel.options[sel.selectedIndex].value; if (value == "Rehab") { if (!(document.getElementBy
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');
}
如果要检查其中一个是否为false,应使用
&&
,并将代码移动到else
块中否,第二个If
并不总是执行下一条语句。如果两个值都是“长期护理”,则它将执行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;
}
}
<input type="text" id="entryType" value="Long Term Care" />
<input type="text" id="followupEntryType" value="Long Term Care" />
你是说用
&&
而不是|
?顺便说一句,不缩进肯定会花费太多时间试图找出代码不起作用的原因。使用重新格式化代码如果有助于更好地理解它,可以将条件重构为!(document.getElementById('entryType')。值==“长期护理”和&document.getElementById('followupEntryType')。值==“长期护理”)
!A | | |!B==!谢谢,我试试你的建议。我想,我可以通过以下简单的方式来编写代码:抱歉,但我以后必须继续这样做。在查看函数getComboB(sel)之前的代码后,我意识到我有一个逻辑错误。然而,我确实将if语句更改为这个语句,并且在代码中进行了更改,这个if可以工作。我感谢所有对此问题提出建议的人。他们都提供了良好的照明。
if (!(document.getElementById('entryType').value == "Long Term Care") && !(document.getElementById('followupEntryType').value == "Long Term Care")) {