【问题标题】:How can I add Class to Table Row when Checkbox is Checked?选中复选框时如何将类添加到表格行?
【发布时间】:2019-01-22 22:16:07
【问题描述】:

我正在尝试添加一个类(以便我以后可以设置样式)表行,其中选中了复选框(JQuery 和 Google Apps 脚本)

目前,我有一些在使用按钮时可以使用的功能。但是,当我尝试修改它们时,它们并没有按预期工作。我什至尝试过.addCss,但没有奏效。当我检查控制台时,我看不到曾经添加过一个类。我曾经让它工作过,但现在似乎无法回到那个点!

我看到 THIS 并对其进行了修改,但 css 没有显示出来。

相关代码

<script>
 //BUTTONS
 $(document).ready(function () {
    $("#tbody tr").on('input', function () {
      var checkbox= $(this).closest('tr').find('[type="checkbox"]');
      checkbox.prop('checked', $(this).val());
    });

   //HIDE UNCHECKED ROWS
    $("#clicker").click(function () {
      $('[type="checkbox"]:not(:checked)').closest('tr').hide();
      return false;
    });

    //SHOW ALL ROWS
     $("#show").click(function () {
      $('[type="checkbox"]:not(:checked)').closest('tr').show();
      return false;
    });

    //CLEAR ALL CHECK MARKS
     $("#clear").click(function () {
      $('[type="checkbox"]').removeAttr('checked');
      return false;
    });

});
 </script>


<script>
//CURRENT ATTEMPT
//I can check all boxes, but styling doesn't work. 
   function setClass() {
  var checkboxes = $('tbody').find('.checkbox');
  checkboxes.closest('tr').toggleClass('on', false);
  checkboxes.not('.check_all').filter(':checked').closest('tr').toggleClass('on', true);
}
$(function($) {
  $(".check_all").on('change', function() {
    var checkAll = $(this).prop("checked");
    $(".checkbox").prop('checked', checkAll);
    var checkboxes = $('tbody').find('.checkbox')
      .eq(0).trigger('setclass');
  });
  var checkboxes = $('tbody').find('.checkbox');
  checkboxes.on('change', function() {
    if ($(this).prop("checked")) {
      $(".check_all").prop('checked', false);
    }
    if ($('.checkbox:checked').length == $('.checkbox').length) {
      $(".check_all").prop('checked', true);
    }
    $(this).trigger('setclass');
  });
  checkboxes.on('setclass', setClass);
  checkboxes.eq(0).trigger('setclass');
});
</script>

风格

<style>
  table, td {
  border: 1px solid black;
  border-collapse: collapse;
}
//CELL WIDTH
td {
   min-width:100px;
}
//TABLE HEAD
th {
   font-size: 20px;
   border: 2px solid black;
   background: #66e9ff;
   border-collapse: collapse;
}
//STYLING FOR CHECKED ROW
 tr.on td {
   background: #ffbbbb;
 }
//STYLING FOR CHECKED ROW
 .on>td {
   color: dark;
 }
//STYLING FOR HOVERING OVER ROW
#tbody tr:hover {
    background-color:yellow;
}
//SELECTABLE
 #feedback { font-size: 1.4em; }
  #table .ui-selecting { background: #FECA40; }
  #table .ui-selected { background: #928bff; color: white; }
  #table { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #table tr { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
</style>
  1. 如何在选中复选框的表格行中添加一个类?
  2. 子问题,我应该以不同的方式编写我的其他复选框功能吗?

【问题讨论】:

  • 请提供一个最小、完整和可验证的示例:stackoverflow.com/help/mcve
  • 两件事,我看到input,这不是事件类型。我想你的意思是change。还有两个,stackoverflow.com/questions/6899859/… 的可能重复项
  • @Twisty input 是有效的事件类型。这就像改变,但你不必先模糊。
  • @Taplar 是使用&lt;tr&gt; 元素的事件吗?
  • 如果事件是从嵌套的孩子冒出来的,是的

标签: jquery jquery-ui google-apps-script


【解决方案1】:

我认为这就是您要寻找的。考虑以下代码。

$(function() {

  function setCheck(o, c) {
    o.prop("checked", c);
    if (c) {
      o.closest("tr").addClass("checked");
    } else {
      o.closest("tr").removeClass("checked");
    }
  }

  function setCheckAll(o, c) {
    o.each(function() {
      setCheck($(this), c);
    });
    if (c) {
      $(".check_all").prop("title", "Check All");
    } else {
      $(".check_all").prop("title", "Uncheck All");
    }
  }

  $(".check_all").on('change', function() {
    setCheckAll($("tbody input[type='checkbox']"), $(this).prop("checked"));
  });
  $("tbody tr").on("click", function(e) {
    var chk = $("[type='checkbox']", this);
    setCheck(chk, !$(this).hasClass("checked"));
  });
});
table {
  border: 1px solid black;
  border-collapse: collapse;
}

#table {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 60%;
}

#table tr {
  margin: 3px;
  padding: 0.4em;
  font-size: 1.4em;
  height: 18px;
}

thead tr th {
  background-color: #66e9ff;
  font-size: 20px;
  border: 2px solid black;
  border-collapse: collapse;
}

tbody td {
  min-width: 100px;
  border: 1px solid black;
}

tbody td:first-child {
  text-align: center;
}

tbody tr.checked td {
  background-color: #ffbbbb;
  color: dark;
}

tbody tr:hover {
  background-color: yellow;
}

#feedback {
  font-size: 1.4em;
}

table .ui-selecting {
  background: #FECA40;
}

table .ui-selected {
  background: #928bff;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th><input type="checkbox" class="check_all" value="Check All"></th>
      <th>Header 1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox"></td>
      <td>Label 1</td>
    </tr>
    <tr>
      <td><input type="checkbox"></td>
      <td>Label 2</td>
    </tr>
    <tr>
      <td><input type="checkbox"></td>
      <td>Label 3</td>
    </tr>
    <tr>
      <td><input type="checkbox"></td>
      <td>Label 4</td>
    </tr>
  </tbody>
</table>

基本上我们有两种情况。用户检查单个项目或用户选择检查所有。对于选中的项目,它应该使用样式类更新父级&lt;tr&gt;。这应该是选中或未选中的单个项目,或所有项目的情况。

我为单个对象创建了一个小函数来完成此操作。然后我制作了一个函数,可以为“检查所有”场景迭代所有这些函数。我离开了.toggleClass(),因为它可能会混淆单次检查和全部检查。

更新

我将上面的代码更新为单击行而不是单击复选框。

希望这会有所帮助。

【讨论】:

  • 1.检查所有作品。 2. 我的“清除”按钮不再起作用。 3. 选中时,我无法对各个行进行格式化。 A. 我发现通过添加您使用的script src,我的.selectable 不再起作用。如果我省略了您使用的script src.selectable 可以工作。 B. 在 JS Fiddle (jsfiddle.net/nateomardavis/e9sk2mt3/7) 中,.selectable 根本不起作用,但单击一行以突出显示红色可以。但是,我无法选中单元格中的复选框。
  • 所以在一个完美的世界里,.selectable. 和点击复选框都以不同的方式工作。 .selectable 突出显示紫色(就像以前一样)。单击复选框或“选中所有”会突出显示红色。
  • 我应该在小提琴中添加一个表格行,因为它在我的最后格式化。在我的实际 Web 应用程序中,&lt;tbody&gt; 是通过 DOM 从数组创建并附加到 &lt;table&gt;,但最终看起来像这样。
  • @N.O.Davis 你的小提琴有很多问题。 jQuery 库必须在 UI 之前加载。您不应使用多个版本。您有大量不需要的 &lt;script&gt; 块。
  • @N.O.Davis tableArray from buildTable(tableArray) 定义在哪里?
猜你喜欢
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2014-03-03
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多