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

✅ 一、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');
	        }
	    }
相关推荐
钛态12 小时前
前端安全防线:CSRF 攻击链路与双重 Token 校验的工程实现
前端·vue·react·web
Java搬码工13 小时前
CompletableFuture 完整详解 + 全场景使用案例
java
许心月13 小时前
Java学习资料网站汇总
java
c2385613 小时前
第二篇:《测试指挥官:可视化单题自测框架(含 assert 实操)》
java·数据库·c++·算法·安全性测试
张拭心13 小时前
技术管理笔记:让我“燃尽”的一天
前端
帅次14 小时前
Kotlin 与 Java 互操作:混合工程里的平台类型与 API 边界
java·开发语言·kotlin·suspend·nullable
X-⃢_⃢-X14 小时前
一、第一阶段:认识 Spring Boot
java·spring boot·后端
前端工作日常14 小时前
我学习到的Java程序的生命周期
java·后端
不好听61314 小时前
BFF 架构实战:从前端直调 API 到加入中间层
前端·架构
SunnyDays101114 小时前
Java 实现 PDF 页面删除、排序、旋转和裁剪
java·pdf