【问题标题】:Change element based on a option from select根据 select 中的选项更改元素
【发布时间】:2018-04-10 10:56:04
【问题描述】:

我有一张非常简单的桌子。我正在尝试根据 HIST 列的值隐藏行。

基本上:

  • 如果选择选择否,则仅显示 HIST = false 的行
  • 如果选择说是,则显示所有行(无论是假还是真)

我被困在这里,但我不是很远。有什么帮助吗?

$('#historySelect').change(function() {
  var option = $(this).val();
  if (option === "No") {

  } else {

  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label>Show History
    <select id="historySelect">
        <option value="Yes">No</option>
        <option value="No">Yes</option>
    </select>
</label>

<table class="table dataTable no-footer" id="dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
  <thead>
    <tr role="row">
      <th class="text-center text-primary sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 66px;">Name</th>
      <th class="text-center text-primary hidden sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Historic: activate to sort column ascending" style="width: 0px;">Historic</th>
    </tr>
  </thead>
  <tbody>
    <tr class="text-center hover-table odd" role="row">

      <td>Name</td>
      <td class="hist hidden">true</td>
    </tr>
    <tr class="text-center hover-table odd" role="row">
      <td>Name</td>
      <td class="hist hidden">false</td>
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    首先请注意,您已交换了 option 元素的值和文本。

    要实现您的要求,您可以将has():contains 选择器结合使用,以获取具有包含false.hist 元素的tr 元素。然后你可以根据需要show()hide(),像这样:

    $('#historySelect').change(function() {
      var $rows = $('#dataTable tbody tr');
      if ($(this).val() === "No") {
        $rows.hide().has('.hist:contains(false)').show();
      } else {
        $rows.show();
      }
    }).change();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <label>Show History
      <select id="historySelect">
        <option value="No">No</option>
        <option value="Yes">Yes</option>
      </select>
    </label>
    
    <table class="table dataTable no-footer" id="dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
      <thead>
        <tr role="row">
          <th class="text-center text-primary sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 66px;">Name</th>
          <th class="text-center text-primary hidden sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Historic: activate to sort column ascending" style="width: 0px;">Historic</th>
        </tr>
      </thead>
      <tbody>
        <tr class="text-center hover-table odd" role="row">
          <td>Name</td>
          <td class="hist hidden">true</td>
        </tr>
        <tr class="text-center hover-table odd" role="row">
          <td>Name</td>
          <td class="hist hidden">false</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • $rows 是什么意思?我从来没有见过。我通常使用var rows
    • $ 前缀是一种命名约定,表明该变量包含一个 jQuery 对象。
    • 没问题,很高兴为您提供帮助
    • 现在我意识到你改变了$rows.hide().filter(':contains(false)').show();它可以工作
    • 我改变了它,这是更具体的。如果 any td 包含 false,以前的版本会隐藏该行
    【解决方案2】:

    您可以使用:contains() 选择器(区分大小写。)。

    $('#historySelect').change(function() {
      var option = $(this).val();
      if (option === "No") {
        $(".hist:contains('false')").parent().hide();
        $(".hist:contains('true')").parent().show();
        
      } else {
        $(".hist:contains('true')").parent().hide();
        $(".hist:contains('false')").parent().show();
        
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <label>Show History
        <select id="historySelect">
            <option value="Yes">No</option>
            <option value="No">Yes</option>
        </select>
    </label>
    
    <table class="table dataTable no-footer" id="dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
      <thead>
        <tr role="row">
          <th class="text-center text-primary sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 66px;">Name</th>
          <th class="text-center text-primary hidden sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Historic: activate to sort column ascending" style="width: 0px;">Historic</th>
        </tr>
      </thead>
      <tbody>
        <tr class="text-center hover-table odd" role="row">
          <td>Name</td>
          <td class="hist hidden">true</td>
        </tr>
        <tr class="text-center hover-table odd" role="row">
          <td>Name</td>
          <td class="hist hidden">false</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 2012-02-11
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多