禁用html表的最后一行复选框

时间:2015-09-09 11:59:57

标签: javascript jquery

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

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;

2 个答案:

答案 0 :(得分:4)

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

&#13;
&#13;
$('#table_forms tr:last input[type="checkbox"]').prop('disabled', true);
&#13;
<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>
&#13;
&#13;
&#13;

&#13;
&#13;
$('#table_forms tr:last input[type="checkbox"]')[0].disabled = true;
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个:

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