jQuery动态添加表格

来源:互联网 发布:php 视频防盗链 编辑:程序博客网 时间:2024/06/06 09:07
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>动态操作表格</title>  
  6. <script type="text/javascript" src="table/jquery.js"  
  7.     language="javascript"></script>  
  8. <script type="text/javascript">  
  9.     var count=0;  
  10.     function add(){  
  11.         var name=$("#name").val();  
  12.         var card=$("#card").val();  
  13.         var sex=$("#sex").val();  
  14.         //判断只能添加二十个  
  15.     if(count==20){  
  16.         alert("不能再加了");  
  17.     }else{  
  18.     count++;  
  19.     //追加  
  20.     $("table").append("<tr id="+count+"><td>"+name+"</td>"+  
  21.         "<td>"+card+"</td>"+"<td>"+sex+"</td>"+"<td>  
  22.         <a href='javascript:del ("+count+")'>删除</a></td></tr>");  
  23.     }  
  24. }  
  25. //删除  
  26. function del(row){  
  27.     count--;  
  28.     $("#"+row).remove();  
  29. }  
  30. </script>  
  31. <style type="text/css">  
  32. table {  
  33.     border: 1px solid #00C;  
  34.     border-collapse: collapse;  
  35.     width: 600px;  
  36.     text-align: center;  
  37. }  
  38.   
  39. table tr td {  
  40.     border: 1px solid #00C;  
  41.     border-collapse: collapse;  
  42.     width: 600px;  
  43. }  
  44. </style>  
  45. </head>  
  46. <body>  
  47.     名字  
  48.     <input type="text" id="name" /> 学号  
  49.     <input type="text" id="card" /> 性别  
  50.     <input type="text" id="sex" />  
  51.     <input type="button" onclick="add()" value="添加" />  
  52.     <p></p>  
  53.     <table>  
  54.         <tr>  
  55.             <th>姓名</th>  
  56.             <th>学号</th>  
  57.             <th>性别</th>  
  58.             <th>操作</th>  
  59.         </tr>  
  60.     </table>  
  61. </body>  
  62. </html>  
0 0
原创粉丝点击