2015-02-11 30 views
0

即時嘗試隱藏包含文本的所有行使用jquery從下拉列表中獲取,但我只是不能讓它工作。 這是功能:隱藏在表中的所有行,除了包含數據從rdropdown

$(document).ready(function(){ 
    $('select[name=selectedName]').change(function() { 
     $('tr').filter(function() { 
      return $(this).find('td').filter(function() { 
       return $(this).text().indexOf('$('select[name=select2]').val()') == -1; 
      }).length; 
     }).$(this).parent("tr:first").hide(); 
    }); 
}); 

,這是下拉:

$query = "SELECT user_name FROM users"; 
$result = mysql_query($query); ?> 
<select name="selectedName" id="userSelected"> 
    <option value="" disabled selected>user name</option> 
    <?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> 
     <option value="<?php echo $line['user_name'];?>"> 
      <?php echo $line['user_name'];?> 
     </option> 
    <?php } ?> 
</select> 

任何想法,爲什麼沒有它的工作嗎?

UPDATE

這是我如何創建表:

<table class="table table-bordered table-hover" ng-controller="tableCtrl" ng-controller="Ctrl"> 
    <thead> 
     <th>user name</th> 
     <th>script name</th> 
     <th>cron format</th> 
    </thead> 
    <tbody ng-repeat="(user_id, row) in data | filter:search"> 
     <tr ng-repeat="(script_id, cron_format) in row "> 
      <td class="userName">{{user(user_id)}}</td> 
      <td class="scriptName">{{script(script_id)}}</td> 
      <td class="cronFormat"><span contenteditable="true" ng-repeat="l in letters(cron_format) track by $index">{{l}}</span><button class="save" ng-click="saveCron(user_id,script_id,cron_format)">save</button></td> 
     </tr> 
    </tbody> 
</table> 

,其結果是有三列和下拉包括所有USER_NAME

+0

您可以張貼表HTML和要發生什麼的例子嗎? – Wilmer 2015-02-11 19:59:02

+0

是肯定一秒 – user3194267 2015-02-11 20:25:24

+0

好吧,你想隱藏包含所選用戶名的行是嗎? – Wilmer 2015-02-11 20:40:27

回答

0

你幾乎有需要的只是一個表改變幾件事:

  • indexOf('$('select[name=select2]').val()')具有無效的撇號位置;
  • $('select[name=select2]').val()應該是$('select[name=selectedName]').val();

  • 最後一部分.$(this).parent("tr:first").hide();無效,但邏輯正確,過濾器函數將返回不需要的行,然後您可以隱藏它們;

  • $(this).find('td')可以更改爲$(this).find('td.userName'),因爲我們只需要用class「userName」搜索單元格。

最終的結果應該是以下幾點:

$(document).ready(function(){ 
    $('select[name=selectedName]').change(function() { 
     $('tr').filter(function() { 
      return $(this).find('td.userName').filter(function() { 
       return $(this).text().indexOf($('select[name=selectedName]').val()) == -1; 
      }).length; 
     }).hide(); 
    }); 
}); 
+0

非常感謝你... wiered問題thoguh ...過濾器只工作第一次我運行它...任何想法爲什麼? – user3194267 2015-02-11 22:19:36

+0

現在這可能是一個不同的問題,發佈一個新的,讓更多的人可以幫助你。 – Wilmer 2015-02-12 10:34:26

相關問題