2015-02-11 25 views
0

我試圖根據選定的值在下拉菜單中顯示和隱藏行。用戶選擇user_name後,我可以使用按鈕在表格中隱藏該用戶行,但是我希望在選擇後顯示並隱藏行,而不必單擊按鈕,我無法使其工作。使用jquery函數和下拉列表選擇

這是一個按鈕,我的工作功能:

$(document).ready(function() { 
    $("button").click(function() { 
     $("td").each(function(index, paragraph) { 
      $td = $(paragraph); 
      if ($td.html() === $('select[name=select1]').val()) { 
       //hide the matched row rather than remove it 
       $(this).parent("tr:first").hide(); 
      } 
     }); 
     $('select[name="select1"]').on('change', function() { 
      $("tr").show(); 
     }); 
    }); 
}); 

這是試圖避免按鈕使用後沒有工作職能:

$(document).ready(function() { 
    $("selectedName").change(function() { 
     $("td").each(function(index, paragraph) { 
      $td = $(paragraph); 
      if ($td.html() === $('select[name=select1]').val()) { 
       //hide the matched row rather than remove it 
       $(this).parent("tr:first").hide(); 
      } 
     }); 
     $('select[name="select1"]').on('change', function() { 
      $("tr").show(); 
     }); 
    }); 
}); 

我在做什麼錯在第二個?

+0

如果你也顯示你的HTML,我們可以更容易地幫你 – dave 2015-02-11 16:06:28

+1

你是指'$(「。selectedName」).change()'還是'$(「#selectedName」)。第二個代碼片段? – 2015-02-11 16:06:48

回答

0

你的選擇是不是在第二個代碼塊有效:

$("selectedName")如果選擇的名稱是一個類,那麼你需要$(".selectedName"),或者如果它的一個ID,然後$("#selectedName")