无法检查JavaScript中的所有复选框

ric*_*zoe 2 checkbox jquery

我在Web应用程序上工作,在运行时,我的代码甚至无法工作.但如果这样做

$("input[name^='show-points-']").attr("checked", false);
Run Code Online (Sandbox Code Playgroud)

在控制台上,复选框取消选中.

奇怪,因为,总是在控制台:

$("input[name^='show-points-']").attr("checked", "checked");
Run Code Online (Sandbox Code Playgroud)

不行.

在这个Jsfiddle中,代码适用于"取消选中"功能,但不适用于"全部检查"功能.

Tus*_*har 7

使用 prop()

更新小提琴

$("#show-all-points").change(function () {
    $("input[name^='show-points-']")
        .prop("checked", this.checked);
});
Run Code Online (Sandbox Code Playgroud)

$("#show-all-points").change(function() {
  $("input[name^='show-points-']")
    .prop("checked", this.checked);
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<tbody>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-pb">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-pi1">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-pi2">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-gp">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-ok">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-op">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" checked="checked" name="show-points-er">
    </td>
  </tr>
  <tr>
    <td></td>
    <td>Tout sélectionner/désélectionner
      <input type="checkbox" checked="checked" id="show-all-points">
    </td>
  </tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)

  • @rickozoe`his.checked`返回`#show-all-points`复选框的选中状态,返回'true/false`,并为所有其他checkboxex设置相同的值. (2认同)