根据选择值过滤表编号的行

时间:2020-02-14 02:30:45

标签: javascript jquery html

例如:Choose 4 -> display 4 rows, choose 6 -> display 6 rows

1 个答案:

答案 0 :(得分:2)

选项值将是数字而不是字符串

<option value="4">4</option>不是

<option value="a1">4</option>

$('.filter-handle').on('change', function(e) {
  var $rows =  $('#table').find('tr');
  var showRow = Number(e.target.value)+1;
  $rows.hide().slice(0, showRow).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="filter-handle">
  <option value="4">4</option>
  <option value="6">6</option>
  <option value="8">8</option>
</select>

<table class="filter-table-data" id="table">
  <tr><th scope="col">Name</th><th scope="col">Des</th></tr>
  <tr><td>Vu Tran</td><td>Sunnyvale</a></tr>
  <tr><td>Michelle Case</td><td>Sunnyvale</a></tr>
  <tr><td>Todd Linden</td><td>Madison</a></tr>
  <tr><td>Michael Liston</td><td>Madison</a></tr>
<tr><td>Vu Tran</td><td>Sunnyvale</a></tr>
  <tr><td>Michelle Case</td><td>Sunnyvale</a></tr>
  <tr><td>Todd Linden</td><td>Madison</a></tr>
  <tr><td>Michael Liston</td><td>Madison</a></tr>
<tr><td>Vu Tran</td><td>Sunnyvale</a></tr>
  <tr><td>Michelle Case</td><td>Sunnyvale</a></tr>
  <tr><td>Todd Linden</td><td>Madison</a></tr>
  <tr><td>Michael Liston</td><td>Madison</a></tr>
</table>