点击数据行选中复选框-抽离公共方法

✅ 一、HTML

复制代码
<table class="table table-border table-bordered table-hover table-bg table-sort">
                    <thead>
                       <tr class="text-c">
                        	<th width="25">
                          <!-- 复选框添加 点击事件 -->
				                <input type="checkbox" id="checkAll" onclick="toggleAll(this)">
				            </th>
                            <th width="80">姓名</th>
                            <th width="150">身份证号</th>
                            <th width="150">工作单位</th>
                            <th width="150">状态</th>
                            <th width="150">审核人</th>
                            <th width="150">审核时间</th>
                            <th width="150">审核状态</th>
                            <th width="150">创建时间</th>
                            <!-- <th width="80">操作</th> -->
                         </tr>
                    </thead>
                    <tbody>
                        #if(userList)
                         #for(ZZMarketUserInfo user : userList)
                         <!-- 数据行 添加 点击事件 -->
                         <tr class="text-c" onclick="toggleRow(this)" >
                           <td>
                           		<!-- 复选框 阻止 checkbox 事件冒泡 -->
				                        <input type="checkbox" name="userIds" value="${user.id}" onclick="event.stopPropagation()">
				                    </td>
		                            <td>${user.name}</td>
		                            <td>${user.idNum}</td>
		                            <td>${user.theDept}</td>
		                            <td>
		                                #if(user.theStatus == 1)已删除#end
		                                #if(user.theStatus == 0)正常#end
		                            </td>
		                            <td>${user.checkUser.name}</td>
		                            <td>${user.checkTime}</td>
		                            <td>
		                            	#if(user.checkStatus == 0)
                           待审批
		                            	#end
		                            	#if(user.checkStatus == 1)
                           审批通过
		                            	#end
		                            	#if(user.checkStatus == 2)
                           审批不通过
		                            	#end

		                            </td>
		                            <td>${user.createTime}</td>
		                           <!--  <td class="td-manage">
		                                <a title="编辑" href="javascript:;" onclick="editUser(${user.id})" class="ml-5"
                                      style="text-decoration:none">
		                                    <i class="Hui-iconfont"></i>
		                                </a>
		                                <a title="删除" href="javascript:;" onclick="deleteUser(${user.id})" class="ml-5"
                                      style="text-decoration:none">
		                                    <i class="Hui-iconfont"></i>
		                                </a>
		                            </td> -->
                                          </tr>
	                        #end
                        #end
                    </tbody>
                </table>

✅ 二、通用 JS(核心复用代码)

复制代码
// 全选/反选功能
	    function toggleAll(source) {
	        const checkboxes = document.getElementsByName('userIds');
	        for (let i = 0; i < checkboxes.length; i++) {
	            checkboxes[i].checked = source.checked;
	        }
	    }
	 	// 点击行 选中/取消 复选框
	    function toggleRow(tr) {
	        // 获取当前行里的复选框
	        let checkbox = tr.querySelector('input[type="checkbox"]');
	        if (checkbox) {
	            checkbox.checked = !checkbox.checked; // 取反
	            // 切换高亮样式
	            tr.classList.toggle('selected');
	        }
	    }
相关推荐
小小小小宇4 分钟前
设计稿转代码:如何将生成代码与内部组件库关联
前端
七牛云行业应用4 分钟前
别每个 AI 工具单独配了!MCP 一次搭建,Claude、Cursor、TRAE 全能用
前端
_xaboy5 分钟前
FormCreate 设计器 v6.3 正式发布:AI 表单助理3.0登场!
前端·vue.js·低代码·开源·表单设计器
胡志辉6 分钟前
邮件中点击“加载图片”,你的IP地址已经被泄漏
前端·后端·安全
luck_bor15 分钟前
集合进阶(Collections Set List)
java
敲敲千反田17 分钟前
Spring AI
java·人工智能·spring
openKaka_18 分钟前
reconcileChildren 深入:React 如何根据 ReactElement 构建子 Fiber
前端·javascript·react.js
拽着尾巴的鱼儿30 分钟前
spring 动态代理
java·后端·spring
三翼鸟数字化技术团队33 分钟前
事件循环原来这么简单!
前端
gf132111134 分钟前
python_【更新已发送的消息卡片】
java·前端·python