HTML 行表选择

HTML Row table selection

本文关键字:选择 HTML      更新时间:2023-09-26

我有一个表格,其中第一列是一个复选框,用于选择当前行,标题有一个 SelectAll 复选框。当用户单击该行时,该表也会被选中(复选框变为选中状态)。

我的问题是,当我全选时,所有复选框都被选中,但行也没有被选中。

我的复选框具有更改功能,因此我假设当我点击全选时,更改方法也会执行,但这不会发生。

$(document).ready(function() {
  $('#selectAll').click(function(event) {
    if (this.checked) {
      // Iterate each checkbox
      $(':checkbox').each(function() {
        this.checked = true;
      });
    } else {
      $(':checkbox').each(function() {
        this.checked = false;
      });
    }
  });
  $('.record_table tr').click(function(event) {
    if (event.target.type !== 'checkbox') {
      $(':checkbox', this).trigger('click');
    }
  });
  $("input[type='checkbox']").change(function(e) {
    if ($(this).is(":checked")) {
      $(this).closest('tr').addClass("highlight_row");
    } else {
      $(this).closest('tr').removeClass("highlight_row");
    }
  });
});
.record_table {
  width: 100%;
  border-collapse: collapse;
}
.record_table tr:hover {
  background: #eee;
}
.record_table td {
  border: 1px solid #eee;
}
.highlight_row {
  background: #eee;
}
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<table class="record_table" id="table-patient-time">
  <tr class="header">
    <th>
      <input type="checkbox" id="selectAll" />
    </th>
    <th>Hello</th>
    <th>Hello</th>
  </tr>
  <tr class="selectable">
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
</table>

JS小提琴在这里

您可以通过在点击事件中触发更改事件来解决此问题:

  $('#selectAll').click(function(event) {
    if (this.checked) {
      // Iterate each checkbox
      $(':checkbox').each(function() {
        this.checked = true;
      }).change();
    } else {
      $(':checkbox').each(function() {
        this.checked = false;
      }).change();
    }
  });

$(document).ready(function() {
  $('#selectAll').click(function(event) {
    if (this.checked) {
      // Iterate each checkbox
      $(':checkbox').each(function() {
        this.checked = true;
      }).change();
    } else {
      $(':checkbox').each(function() {
        this.checked = false;
      }).change();
    }
  });
  $('.record_table tr').click(function(event) {
    if (event.target.type !== 'checkbox') {
      $(':checkbox', this).trigger('click');
    }
  });
  $("input[type='checkbox']").change(function(e) {
    if ($(this).is(":checked")) {
      $(this).closest('tr').addClass("highlight_row");
    } else {
      $(this).closest('tr').removeClass("highlight_row");
    }
  });
});
.record_table {
  width: 100%;
  border-collapse: collapse;
}
.record_table tr:hover {
  background: #eee;
}
.record_table td {
  border: 1px solid #eee;
}
.highlight_row {
  background: #eee;
}
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<table class="record_table" id="table-patient-time">
  <tr class="header">
    <th>
      <input type="checkbox" id="selectAll" />
    </th>
    <th>Hello</th>
    <th>Hello</th>
  </tr>
  <tr class="selectable">
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
</table>

工作小提琴

您可以添加类highlight_row并在单击事件中删除selectAll

$('#selectAll').click(function(event) {
    if(this.checked) {
      // Iterate each checkbox
        $(':checkbox').each(function() {
          this.checked = true;
          $(this).closest('tr').addClass('highlight_row');
        });
    }
    else {
        $(':checkbox').each(function() {
          this.checked = false;
          $(this).closest('tr').removeClass('highlight_row');
        });
    }
});

希望这有帮助。


$(document).ready(function () {
    $('#selectAll').click(function(event) {
        if(this.checked) {
          // Iterate each checkbox
            $(':checkbox').each(function() {
              this.checked = true;
              $(this).closest('tr').addClass('highlight_row');
            });
        }
        else {
            $(':checkbox').each(function() {
              this.checked = false;
              $(this).closest('tr').removeClass('highlight_row');
            });
        }
    });
  $('.record_table tr').click(function (event) {
    if (event.target.type !== 'checkbox') {
      $(':checkbox', this).trigger('click');
    }
  });
  $("input[type='checkbox']").change(function (e) {
    if ($(this).is(":checked")) {
      $(this).closest('tr').addClass("highlight_row");
    } else {
      $(this).closest('tr').removeClass("highlight_row");
    }
  });
});
.record_table {
    width: 100%;
    border-collapse: collapse;
}
.record_table tr:hover {
    background: #eee;
}
.record_table td {
    border: 1px solid #eee;
}
.highlight_row {
    background: #eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="record_table" id="table-patient-time">
<tr class="header">
        <th>
            <input type="checkbox" id= "selectAll" />
        </th>
        <th>Hello</th>
        <th>Hello</th>
    </tr>
    <tr class="selectable">
        <td>
            <input type="checkbox" />
        </td>
        <td>Hello</td>
        <td>Hello</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" />
        </td>
        <td>Hello</td>
        <td>Hello</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" />
        </td>
        <td>Hello</td>
        <td>Hello</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" />
        </td>
        <td>Hello</td>
        <td>Hello</td>
    </tr>
</table>

我只会触发更改,而不是在多个地方有逻辑

$('#selectAll').click(function(event) {
    $(':checkbox').prop("checked",this.checked).change();
});
$("input[type='checkbox']").change(function (e) {
    $(this).closest('tr').toggleClass("highlight_row", this.checked);
});

现在我要为 html 做的是你应该使用 thead 和 tbody,而不是使用一个类来表示你有一个 head 行。

JsFiddle

对工作示例进行了小调整,将click事件调度到需要更改的复选框。这应该会正确触发这些框的 onchange 事件

新部分如下所示:

if(this.checked) {
// Iterate each checkbox
  $(':checkbox').not(":checked").click()
}
else {
  $(':checkbox:checked').click()
}