2017-09-05 94 views
-2

的Javascript的if-else語句我有一個JavaScript函數隱藏行,這取決於哪個按鈕被點擊它會顯示是否匹配的錶行上嵌套函數

$("#example").on("click", function() 
{ 
    var $rowsNo = $("#table tbody tr").hide().filter(function() { 
    return $.trim($(this).find("td").eq(0).text()) === "4" 
}).show(); 
}); 

這工作得很好含量根據。但我想添加一個if聲明,稱如果它等於4顯示的內容,如果它不等於4,顯示內容爲5.我已經試過

$("#example").on("click", function() 
{ 

    if ($(this).find("td").eq(0).text()) === "4"){ 
    var $rowsNo = $("#table tbody tr").hide().filter(function() { 
    return $.trim($(this).find("td").eq(0).text()) === "4" 
}).show(); 
} 
else{ 
    var $rowsNo = $("#table tbody tr").hide().filter(function() { 
    return $.trim($(this).find("td").eq(0).text()) === "5" 
}).show(); 

} 
}); 

我得到的

Uncaught SyntaxError: Unexpected token === 
錯誤
+1

我縮進了你的代碼,並找到了一個額外的')'。它應該現在工作 –

+1

@AndreiCACIO張貼你的發現作爲答案...不要修改他的問題。 – brso05

+1

@AndreiCACIO,如果您發現語法錯誤,請勿將其添加到原始文章中。要麼通知作者,要麼將其寫爲答案,如果它回答 – smac89

回答

0

if語句中有一個額外的')'。此版本應該工作:

$("#example").on("click", function() { 
    if (
    $(this) 
     .find("td") 
     .eq(0) 
     .text() === "4" 
) { 
    var $rowsNo = $("#table tbody tr") 
     .hide() 
     .filter(function() { 
     return (
      $.trim(
      $(this) 
       .find("td") 
       .eq(0) 
       .text() 
     ) === "4" 
     ); 
     }) 
     .show(); 
    } else { 
    var $rowsNo = $("#table tbody tr") 
     .hide() 
     .filter(function() { 
     return (
      $.trim(
      $(this) 
       .find("td") 
       .eq(0) 
       .text() 
     ) === "5" 
     ); 
     }) 
     .show(); 
    } 
});