禁用最后一行html表复选框

Disable Last row of html tables checkbox

本文关键字:一行 html 复选框 最后      更新时间:2023-09-26

我试图在两个条件下禁用复选框,如果表中只有一行或最后一行,但它没有得到为什么它没有禁用。

HTML:

<table id="table_forms">
    <tr>
        <td><input type="checkbox" class="chkView"/>View</td>
    </tr>
   <tr>
        <td><input type="checkbox" class="chkView"/>View</td>
    </tr>
</table>

JS:

$('#table_forms tr:last input[type="checkbox"]').disabled;

您可以使用 prop() 设置禁用属性或使用[0]获取dom元素并设置disabled属性

$('#table_forms tr:last input[type="checkbox"]').prop('disabled', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="table_forms">
  <tr>
    <td>
      <input type="checkbox" class="chkView" />View</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="chkView" />View</td>
  </tr>
</table>

$('#table_forms tr:last input[type="checkbox"]')[0].disabled = true;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="table_forms">
  <tr>
    <td>
      <input type="checkbox" class="chkView" />View</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="chkView" />View</td>
  </tr>
</table>

试试这个:

$('#table_forms tr:last input[type="checkbox"]').attr('disabled','disabled');