0

我第一次点击它时,它会点击,但不会关闭...

我试过这个它检查和取消检查,但只有一次。

这个对也不起作用。

$(document).ready(function() {
  $("tr").click(function() {
    var checkbox = $(this).find("input[type=checkbox]");

    if (!checkbox.prop("checked", "")) {
      checkbox.prop("checked", "false");
    } else {
      checkbox.prop("checked", "true");
    }

  });
});
td{
    background:red;
    padding:10px 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>test</td>
  </tr>
</table>

4

1 回答 1

1

这是一个切换

$(document).ready(function() {
  $("tr").on("click", function(e) {
    const $target = $(e.target)
    const $checkbox = $(this).find("input[type=checkbox]");
  // only run code when NOT clicking checkbox
    if (!$checkbox.is($target)) {
      let checked = $checkbox.is(":checked")
      $checkbox.prop("checked", !checked)
    }
  });
});
td {
  background: red;
  padding: 10px 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>test</td>
  </tr>
</table>

于 2021-03-20T13:47:14.570 回答