点击tr中的全选使tr中的所有checkbox全选上

本文详细介绍了如何使用JavaScript实现表格数据的全选按钮功能,包括两种不同的方法及其背后的逻辑,旨在帮助开发者理解并应用到实际项目中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面的代码如下:

<table cellpadding="0" cellspacing="0" id="viewTable" class="tablesorter">
    <thead>
     <tr>
     <th  width="10%"><fmt:message key="projectmember.department.title"/></th>
     <th  width="85%"><fmt:message key="projectmember.userid.title"/></th>
     <th  width="5%"><fmt:message key="operation"/></th>
     </tr>
    </thead>
    <tbody>
     <c:forEach items="${departments}" var="current"  varStatus="i"> 
      <c:choose>
       <c:when test="${(i.count) % 2 == 0}">
           <c:set var="rowclass" value="rowtwo"/>
       </c:when>
       <c:otherwise>
           <c:set var="rowclass" value="rowone"/>
       </c:otherwise>
      </c:choose>
     <tr class="${rowclass}">
      <td>
       ${current.name}
      &nbsp;
      </td>
      <td id="one${current.id}">      
         <c:forEach items="${current.users}" var="user"  varStatus="j">
          <form:checkbox path="userName" value="${user.id}" />${user.cname}&nbsp;
         </c:forEach>                 
      </td>
      <td>
      <input type="button" onclick="test('${current.id}');" value="全选"/>
      </td>
     </tr>
     </c:forEach>
    </tbody>
   </table>

一种方法是<td>
      <input type="button" onclick="test('${current.id}');" value="全选"/>
      </td>

function test(i) { 
  var str="one"+i;
  $("#"+str+" :"+'input[type=checkbox]').attr("checked",true);
         }

另一种方法是<td>
      <input type="checkbox" onclick="test('${current.id}',this);" /> 全选
      </td>

fufunction test(i,checkbox) { 
  var str="one"+i;
  $("#"+str+" :"+'input[type=checkbox]').prop('checked', $(checkbox).prop('checked'));
         }

 

转载于:https://my.oschina.net/u/659230/blog/99931

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值