2013-03-16 1 views
0

나는 다음과 같은 비교 연산자를 작동 시키려고 노력하고있다. 두 번째 'if'는 항상 코드에서 다음 명령문을 실행합니다. 내가 원하는이 eith 입력 유형 또는 followupEntryType 여기 코드는 ...자바 스크립트 아니요 (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

'|| '대신'&&'를 사용합니까? –

+3

그런데 들여 쓰기는 코드가 작동하지 않는 이유를 알아 내려고 너무 많은 시간을 할애 할 수있는 확실한 방법입니다. –

+0

코드를 [this] (http://jsbeautifier.org/) –

답변

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) 함수 앞에 내 코드를 검토 한 결과 논리 오류가 있다는 것을 알았습니다. 그러나이 문장을 변경하면 코드가 변경되면 코드가 변경됩니다. 이 문제에 대한 제안을 한 모든 분들께 감사드립니다. 그들은 모두 좋은 nsight를 제공했습니다. – user2033850

+0

나는이 제안에 찬성표를 던졌다. – user2033850

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

+0

미안하지만, 나는 나중에 이것을 따라야 할 것이다. – user2033850

0

아니, 두 번째 if은 항상 다음 문을 실행하지 않습니다 . 두 값이 모두 "Long Term Care"이면 else 부분의 코드가 실행됩니다. 즉 :

<input type="text" id="entryType" value="Long Term Care" /> 
<input type="text" id="followupEntryType" value="Long Term Care" /> 

데모 : 당신은 조건이 두 값이 모두 "Long Term Care" (중 값이 "Long Term Care" 경우 예는 else로 이동합니다) 다른 경우 사실하려면 http://jsfiddle.net/QW43B/

, 당신은을 사용한다 && 연산자 대신 :

if (!(document.getElementById('entryType').value == "Long Term Care") && !(document.getElementById('followupEntryType').value == "Long Term Care")) {