动态根据checkbox 增加Dom

本文介绍了一个用于批量更新产品信息的网页设计,包括全选功能、输入按钮和表格展示,通过JavaScript实现批量选择和操作。实现了产品的批量更新功能,提供了用户友好的交互体验。
 1 <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeBehind="TestForDemo.aspx.cs" Inherits="Maticsoft.Web.Product.TestForDemo" %>
 2 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
 3     <script src="../js/checkBoxHelper.js"></script>
 4 </asp:Content>
 5 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 6     <input type="button" id="btn_Update" value="批量更新" />
 7     <span>全部选择:<input type="checkbox" name="SelectAll"   /></span>
 8     <table class="table">
 9         <thead>
10             <th>选择</th>            
11             <th>产品Id</th>
12             <th>名称</th>
13             <th>描述</th>
14             <th>生产厂家</th>
15             <th>价格</th>
16             <th>链接</th>
17             <th>标题 </th>
18         </thead>
19         <%=ConsTr %>
20         
21     </table>
22     <script type="text/javascript">
23         $(document).ready(function () {
24             $("input[name='SelectAll']").click(function () {
25                 if ($(this).attr("checked") == "checked") {
26                     checkBoxHelper.allChecked();
27                 }
28                 else {
29                     checkBoxHelper.allCancle();
30                     
31                 }
32             });
33             $("input[name='forTest']").click(function () {
34                 if ($(this).attr("checked") == "checked") {
35                    
36                     checkBoxHelper.addTr(this);
37                 }
38                 else {
39                     var $tr = $(this).parents("tr");
40                     $tr.next(".newrow").remove();
41 
42                 }
43             });
44             $("#btn_Update").click(function () {
45                 if ($(this).val() == "批量更新") {
46                     var check = checkBoxHelper.getChecked();
47                     if (check.length == 0) {
48                         alert("至少选择一个");
49                         return false;
50                     }
51                     $(this).val("保存");                    
52                     $.each(check, function (index, element) {
53                         checkBoxHelper.addTr(element);
54                     });
55                     
56 
57                 }
58                 else {
59                     $(this).val("批量更新");
60                     alert("保存成功");
61                 }
62 
63                
64             });
65         });
66 
67     </script>
68 </asp:Content>
HTML
 1 var checkBoxHelper = {
 2     ///全选
 3     allChecked: function () {
 4         $('input[name="forTest"]').attr("checked", "checked");
 5     },
 6     allCancle: function () {
 7         $("input[name='forTest']").each(function () {
 8             this.checked = !this.checked;
 9         });
10     },
11     getChecked: function () {
12         var check = new Array();
13         $("input[name='forTest']").each(function () {
14             if (this.checked) {
15                 check.push(this);
16             }
17         });
18 
19         return check;
20     },
21     addTr: function (element) {
22         var $tr = $(element).parents("tr");
23         if ($tr.next(".newrow").length>0) {
24             return false;
25         }
26         if ($("#btn_Update").val() == "批量更新") {
27             return false;
28         }
29         var price = $tr.find("#price").text();
30         var select = "<select>";
31         select += "<option>请选择</option>";
32         select += "<option>ywx236602</option>";
33         var $td = "<td>" + select + price + "</td>";
34         var aftertr = $("<tr class='newrow'></tr>");
35         aftertr.append($td);
36         $tr.after(aftertr);
37     }
38 
39 };
JS
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using Maticsoft.Model;
 8 using System.Text;
 9 
10 namespace Maticsoft.Web.Product
11 {
12     public partial class TestForDemo : System.Web.UI.Page
13     {
14         public string ConsTr { get; set; }
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             List<Products> prods = new BLL.Products().GetModelList("");
18             StringBuilder sb = new StringBuilder();
19             sb.Append("<tbody>");
20             foreach (var item in prods)
21             {
22                 sb.Append("<tr>");
23                 sb.AppendFormat("<td><input type='checkbox' name='forTest'  id='{0}'/></td>",item.ProductId);
24                 sb.AppendFormat("<td>{0}</td>", item.ProductId);
25                 sb.AppendFormat("<td>{0}</td>",item.Name);
26                 sb.AppendFormat("<td>{0}</td>",item.Description);
27                 sb.AppendFormat("<td>{0}</td>",item.Catagory);
28                 sb.AppendFormat("<td id='price'>{0}</td>",item.Price);
29                 sb.AppendFormat("<td>{0}</td>",item.Href);
30                 sb.AppendFormat("<td>{0}</td>",item.Title);
31                 sb.Append("</tr>");
32                
33             }
34             sb.Append("</tbody>");
35             ConsTr=sb.ToString();
36         }
37     }
38 }
c#

 

转载于:https://www.cnblogs.com/oran/p/4348835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值