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

✅ 一、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');
	        }
	    }
相关推荐
阿亮爱学代码几秒前
日期与滚动视图
java·前端·scrollview
java1234_小锋几秒前
说说MyBatis的工作原理吗?
java·mybatis
恶猫2 分钟前
自动拨号换ip软件简单实现。aardio版。
java·网络·aardio·adsl·换ip·rasphone.exe·rasdial.exe
欧米欧3 分钟前
STRING的底层实现
前端·c++·算法
2301_8148098611 分钟前
踩坑实战pywebview:用 Python + Web 技术打造轻量级桌面应用
开发语言·前端·python
anzhxu11 分钟前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
lUie INGA12 分钟前
快速在本地运行SpringBoot项目的流程介绍
java·spring boot·后端
LIO14 分钟前
Vue 3 实战——搜索框检索高亮的优雅实现
前端·vue.js
_thought17 分钟前
踩坑记录:Vue Devtools(Vue2版)在火狐浏览器上,未在控制台显示
前端·javascript·vue.js