2011-12-17 22 views
1

我有,當我試圖運行一個簡單的if語句的Javascript如果語法

[打破這個錯誤]})語法錯誤;

無效轉讓左側 [打破這個錯誤]容器+ =

什麼是我的問題 以及我如何使:

if this.ewCount != 0 then {} 
elseif NotDoneh == 0 then {} 
ELSE {} 

這是我當前的代碼:

var conta = '<div>'; 
$.each(items, function() { 
    if (this.ewCount != 0) { 

     if (DoneWidth == 0) { 
      conta += '<br/>dddddddddddddddddd<br/><br/>' + 
     }); 
     if (NotDoneh == 0) { 
      conta += '<br/>dddddddddddddddddd<br/><br/>' + 
     }); 
    }); 

    container += '</div>' + 
+2

'+' - 操作員需要2個項目。你只給一個。 conta + =「one」+ 它應該像conta + =「one」或conta + =「one」+「two」 – poitroae 2011-12-17 10:58:58

回答

0

要利用你的僞代碼:

if this.ewCount != 0 then {} 
elseif NotDoneh == 0 then {} 
ELSE {} 

,並把它轉換成JavaScript:

if (this.ewCount != 0) { 
    // do something 
} else if (NotDoneh == 0) { 
    // do something else 
} else { 
    // do something else again 
} 

在您的實際JS有幾個問題,主要是你已經有了月底+操作一些行之後沒有另一個操作員,並且你已經關閉了你的每條if語句});,他們應該剛好有} - 我想你已經混淆瞭如何關閉$.each,因爲它是一個函數作爲參數調用,因此它需要爲$.each(items,function() { });

我不知道如何重寫你的JS,因爲它有一個if (DoneWidth == 0)測試不在你的僞代碼 - 是否應該嵌套在第一個如果,或...?無論如何,如果你更新它看起來像下面至少應該是有效的,即使不是正確的算法:

$.each(items, function() { 
    if (this.ewCount != 0) { 
     // note the following if is nested inside the one above: not sure 
     // if that's what you intended, but it gives a good example of 
     // how to do something like that 
     if (DoneWidth == 0) { 
      conta += '<br/>dddddddddddddddddd<br/><br/>'; 
     } 
    } else if (NotDoneh == 0) { 
      conta += '<br/>dddddddddddddddddd<br/><br/>'; 
    } else { 
     // you don't seem to have any code to go in the final else, 
     // unless it was meant to be 
     container += '</div>'; 
    } 

    container += '</div>'; 
}); // note here }); is correct because the } closes the anonymous function 
    // and then the); finishes off the $.each(... 

你可以把連同的if/else結構我上面顯示,和/或者以其他方式移動東西,這樣右邊的代碼就會在右邊的內部結束。

2

刪除if塊的尾隨大括號後的括號。

if (NotDoneh == 0) { conta += '<br/>dddddddddddddddddd<br/><br/>' + 
}); 

應該

if (NotDoneh == 0) { conta += '<br/>dddddddddddddddddd<br/><br/>' + 
} // <-- No); <-- This is not a smiley, but a parenthesis + semicolon. 
+0

我該怎麼做ELSE? if this.ewCount!= 0 then {} elseif NotDoneh == 0 then {} ELSE {} – Sveta26 2011-12-17 11:15:19

+0

`if(...){...} else if(....){...} else { ..}是正確的語法。 – 2011-12-17 11:49:08

0
if (this.ewCount != 0) { 
    if (DoneWidth == 0) { 
    conta += '<br/>dddddddddddddddddd<br/><br/>'; 
    } 
    if (NotDoneh == 0) { 
    conta += '<br/>dddddddddddddddddd<br/><br/>'; 
    } 
} 
+0

我該如何做ELSE?如果this.ewCount!= 0 then {} elseif NotDoneh == 0 then {} ELSE {} – Sveta26 2011-12-17 11:15:12

0
var conta = '<div>'; 
    $.each(items, function() { 
     if (this.ewCount != 0) 
     { 
      if (DoneWidth == 0) { 
      conta += '<br/>dddddddddddddddddd<br/><br/>' 
      } 

      if (NotDoneh == 0) { 
      conta += '<br/>dddddddddddddddddd<br/><br/>' 
      } 
     } 

     else{ //here you do the else } 
    }); 
+0

我該如何做ELSE?如果this.ewCount!= 0 then {} elseif NotDoneh == 0 then {} ELSE {} – Sveta26 2011-12-17 11:14:25

0
elseif NotDoneh == 0 then {} 

您不必elseif在JS,你應該使用else if代替:

else if NotDoneh == 0 then {}