代码之家  ›  专栏  ›  技术社区  ›  holden

jquery禁用一个DIV的多个子输入

  •  4
  • holden  · 技术社区  · 14 年前

    我想从下面的复选框中做一些事情,

    每行都有一个复选框,我想在单击该复选框时禁用class.room所在行的所有输入字段。

    function toggleStatus(link) {
        $(link).closest(".room").children(':input').attr('disabled', true);
    }
    

    也试过

    function toggleStatus(link) {
        $(link).closest(".room").children('input[type=text]').attr('disabled', true);
    }
    
    1 回复  |  直到 13 年前
        1
  •  6
  •   Sampson    14 年前

    你的问题有些模棱两可,因此以下可能不是 确切地 你在找什么。

    单击后,您应该遍历到最近的表行,查找具有类名的所有输入 .room 并根据复选框本身的状态设置其禁用属性。

    $(":checkbox").click(function(){
      $(this).closest("tr").find(":input.room")
        .attr("disabled", $(this).is(":checked"));
    });
    

    这假设结构与以下结构类似:

    <table>
      <tbody>
        <tr>
          <td><input type="checkbox" /></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
        </tr>
        <tr>
          <td><input type="checkbox" /></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
          <td><input type="text" class="room" /></td>
        </tr>
      </tbody>
    </table>
    

    在线演示: http://jsbin.com/umimu/edit