2013-02-07 90 views
-8

我有一個與Cold Fusion集成的傳統JS函數,用於輸出包含if語句的第n個ID表單字段。有時輸出非常高,看起來非常冗餘和麻煩。我在想一個for循環會解決這個問題,但不知道解決這個問題的正確方法。以下是來自示例輸出的遺留代碼。任何幫助表示讚賞:這個Javascript函數可以用jQuery重寫嗎?

function checkFinalDisposition(){ 
if (document.getElementById("mrostatus").value == "Completed"){ 


    var checkfinal = document.getElementById("finaldisposition").value 
    if (checkfinal == 'NEGATIVE' || checkfinal == 'NEGATIVE DILUTE'){ 
     var checkfinalpass = 'Yes' 

     if (document.getElementById("id4735721").value != 'NEGATIVE' || document.getElementById("id4735721").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
     if (document.getElementById("id4735722").value != 'NEGATIVE' || document.getElementById("id4735722").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
     if (document.getElementById("id4735723").value != 'NEGATIVE' || document.getElementById("id4735723").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
     if (document.getElementById("id4735724").value != 'NEGATIVE' || document.getElementById("id4735724").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
     if (document.getElementById("id4735725").value != 'NEGATIVE' || document.getElementById("id4735725").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
     if (document.getElementById("id4735726").value != 'NEGATIVE' || document.getElementById("id4735726").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
     if (document.getElementById("id4735727").value != 'NEGATIVE' || document.getElementById("id4735727").value != 'NEGATIVE DILUTE'){ 
      var checkfinalpass = 'No' 
     } 
    } 
    else{ 
     var checkfinalpass = 'No' 
     //Multiple IF's from drug query 

     if (document.getElementById("id4735721").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

     if (document.getElementById("id4735722").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

     if (document.getElementById("id4735723").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

     if (document.getElementById("id4735724").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

     if (document.getElementById("id4735725").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

     if (document.getElementById("id4735726").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

     if (document.getElementById("id4735727").value == checkfinal){ 
      var checkfinalpass = 'Yes' 
     } 

    } 

    if (checkfinalpass != 'Yes'){ 
     var inputfinaldisposition = 'NEGATIVE' 
     //Multiple IF's from drug query 

     if (document.getElementById("id4735721").value == ""){ 
      document.getElementById("id4735721").value = "POSITIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735721").value != 'NEGATIVE' || document.getElementById("id4735721").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735721").value 
     } 

     if (document.getElementById("id4735722").value == ""){ 
      document.getElementById("id4735722").value = "POSITIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735722").value != 'NEGATIVE' || document.getElementById("id4735722").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735722").value 
     } 

     if (document.getElementById("id4735723").value == ""){ 
      document.getElementById("id4735723").value = "NEGATIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735723").value != 'NEGATIVE' || document.getElementById("id4735723").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735723").value 
     } 

     if (document.getElementById("id4735724").value == ""){ 
      document.getElementById("id4735724").value = "NEGATIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735724").value != 'NEGATIVE' || document.getElementById("id4735724").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735724").value 
     } 

     if (document.getElementById("id4735725").value == ""){ 
      document.getElementById("id4735725").value = "NEGATIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735725").value != 'NEGATIVE' || document.getElementById("id4735725").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735725").value 
     } 

     if (document.getElementById("id4735726").value == ""){ 
      document.getElementById("id4735726").value = "NEGATIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735726").value != 'NEGATIVE' || document.getElementById("id4735726").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735726").value 
     } 

     if (document.getElementById("id4735727").value == ""){ 
      document.getElementById("id4735727").value = "NEGATIVE"; 
     } 
     if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735727").value != 'NEGATIVE' || document.getElementById("id4735727").value != 'NEGATIVE DILUTE')){ 
     var inputfinaldisposition = document.getElementById("id4735727").value 
     } 

     var answer = confirm("Please advise the Final Disposition does not match your individual results!\n\nPress \"OK\" to change Final Disposition to "+inputfinaldisposition) 

     if (answer){ 
      document.getElementById("finaldisposition").value = inputfinaldisposition; 
      return true; 
     } 
     else{ 
      return false 
     } 
    } 
} 
} 
+1

[*您嘗試過什麼?*](http://whathaveyoutried.com/) – zzzzBov

+0

第二個@zzzzBov ... –

+1

jQuery IS JavaScript。但是,您可以使用jQuery甚至簡單的舊JavaScript函數縮短一些代碼。 –

回答

1

我將有ID的JS數組你檢查,

idToCheck = ['id4735721', 'id4735722', ...]; 

然後在比較它們的循環。

for(id in idToCheck){ 
     if($('#'+idToCheck[id]).val() != 'NEGATIVE' || if($('#'+idToCheck[id]).val() != 'NEGATIVE DILUTE'){ 
      checkfinalpass = 'No'; 
     } 
} 

我沒有重做你的整個代碼,但希望這會指出你在正確的方向。

+0

*「JS的js對象」*實際上是一個數組,但是這會縮短它並使其更易於維護。 –

+0

你是對的,將解決這個問題 – Falantar014

相關問題