jQuery全选全删动态表格

jQuery全选全删动态表格

1.效果图附上:
在这里插入图片描述

2.代码呈上:



<!DOCTaYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="keywords" content=" keywords" />
<meta name="description" content="description" />
</head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
    body{
   font-size:12px}
    table{
   width:360px;border-collapse:collapse}
    table tr th,td{
   border:solid 1px #666;text-align:center}
    table tr td img{
   border:solid 1px #ccc;padding:3px;width:42px;height:60px;cursor:pointer}
    table tr td span{
   float:left;padding-left:12px}
    table tr th{
   background-color:#ccc;height:32px;background-color:#fff}
    .clsImg{
   position:absolute;border:solid 1px #ccc;padding:3px;background-color:#eee;display:none;cursor:pointer}
    .btn{
   border:solid 1px #666;padding:2px;width:50px;filter:progd:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#fff,E
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于jQuery实现的动态增删改查表格,其中每条数据带有复选框: HTML代码: ```html <table id="myTable"> <thead> <tr> <th><input type="checkbox" id="checkAll"></th> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox" class="checkItem"></td> <td>张三</td> <td>20</td> <td> <button class="editBtn">编辑</button> <button class="deleteBtn">删除</button> </td> </tr> </tbody> </table> <button id="addBtn">新增</button> ``` jQuery代码: ```javascript // 全选/取消全选 $("#checkAll").click(function() { $(".checkItem").prop("checked", this.checked); }); // 单个复选框状态改变时改变全选复选框状态 $(".checkItem").change(function() { if ($(this).prop("checked") === false) { $("#checkAll").prop("checked", false); } else if ($(".checkItem:checked").length === $(".checkItem").length) { $("#checkAll").prop("checked", true); } }); // 点击新增按钮 $("#addBtn").click(function() { var newRow = "<tr>" + "<td><input type='checkbox' class='checkItem'></td>" + "<td><input type='text' class='nameInput'></td>" + "<td><input type='text' class='ageInput'></td>" + "<td><button class='saveBtn'>保存</button>" + "<button class='cancelBtn'>取消</button></td>" + "</tr>"; $("#myTable tbody").append(newRow); }); // 点击保存按钮 $("#myTable").on("click", ".saveBtn", function() { var name = $(this).parents("tr").find(".nameInput").val(); var age = $(this).parents("tr").find(".ageInput").val(); var newRow = "<tr>" + "<td><input type='checkbox' class='checkItem'></td>" + "<td>" + name + "</td>" + "<td>" + age + "</td>" + "<td><button class='editBtn'>编辑</button>" + "<button class='deleteBtn'>删除</button></td>" + "</tr>"; $(this).parents("tr").replaceWith(newRow); }); // 点击取消按钮 $("#myTable").on("click", ".cancelBtn", function() { $(this).parents("tr").remove(); }); // 点击编辑按钮 $("#myTable").on("click", ".editBtn", function() { var name = $(this).parents("tr").find("td:eq(1)").text(); var age = $(this).parents("tr").find("td:eq(2)").text(); var editRow = "<tr>" + "<td><input type='checkbox' class='checkItem'></td>" + "<td><input type='text' class='nameInput' value='" + name + "'></td>" + "<td><input type='text' class='ageInput' value='" + age + "'></td>" + "<td><button class='saveBtn'>保存</button>" + "<button class='cancelBtn'>取消</button></td>" + "</tr>"; $(this).parents("tr").replaceWith(editRow); }); // 点击删除按钮 $("#myTable").on("click", ".deleteBtn", function() { $(this).parents("tr").remove(); }); ``` 以上代码实现了动态增删改查表格,并且每条数据带有复选框。其中,全选/取消全选、单个复选框状态改变时改变全选复选框状态通过jQuery实现;新增、保存、取消、编辑、删除等操作通过绑定jQuery事件实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值