Home > Web Front-end > JS Tutorial > jquery dynamically adds and deletes table row special effects_jquery

jquery dynamically adds and deletes table row special effects_jquery

WBOY
Release: 2016-05-16 15:30:58
Original
1588 people have browsed it

The code for adding and deleting tables based on jQuery is a special effect code for dynamically adding and deleting table rows. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:

html code:

<div style="width:720px;margin:20px auto;">
   <table id="tab11" style="display: none">
     <tbody>
       <tr>
         <td height="30" align="center">
           <input type="text" name="NO" size="2" value="1" />
         </td>
         <td align="center">
           <input type="text" name="start_end_time" />
         </td>
         <td align="center">
           <input type="text" name="unit_department" />
         </td>
         <td align="center">
           <input type="text" name="post" />
         </td>
         <td>
           <input type="button" id="Button1" onclick="deltr(this)" value="删行" />
         </td>
       </tr>
     </tbody>
   </table>
   <input type="button" id="btn_addtr" value="增行" />
   <table id="dynamicTable" width="700" border="0" cellspacing="0" cellpadding="0">
     <thead>
       <tr>
         <td height="30" align="center" bgcolor="#CCCCCC">ID</td>
         <td align="center" bgcolor="#CCCCCC">起止时间</td>
         <td align="center" bgcolor="#CCCCCC">单位/部门</td>
         <td align="center" bgcolor="#CCCCCC">职位</td>
         <td></td>
       </tr>
     </thead>
     <tbody>
       <tr>
         <td height="30" align="center">
           <input type="text" name="NO" size="2" value="1" />
         </td>
         <td align="center">
           <input type="text" name="start_end_time" />
         </td>
         <td align="center">
           <input type="text" name="unit_department" />
         </td>
         <td align="center">
           <input type="text" name="post" />
         </td>
         <td>
           <input type="button" id="Button2" onclick="deltr(this)" value="删行" />
         </td>
       </tr>
     </tbody>
   </table>
 </div>
Copy after login

js code:

$(function () {
      var show_count = 20;  //要显示的条数
      var count = 1;  //递增的开始值,这里是你的ID
      $("#btn_addtr").click(function () {
 
        var length = $("#dynamicTable tbody tr").length;
        //alert(length);
        if (length < show_count)  //点击时候,如果当前的数字小于递增结束的条件
        {
          $("#tab11 tbody tr").clone().appendTo("#dynamicTable tbody");  //在表格后面添加一行
          changeIndex();//更新行号
        }
      });
 
 
    });
    function changeIndex() {
      var i = 1;
      $("#dynamicTable tbody tr").each(function () { //循环tab tbody下的tr
        $(this).find("input[name='NO']").val(i++);//更新行号
      });
    }
 
    function deltr(opp) {
      var length = $("#dynamicTable tbody tr").length;
      //alert(length);
      if (length <= 1) {
        alert("至少保留一行");
      } else {
        $(opp).parent().parent().remove();//移除当前行
        changeIndex();
      }
    }
Copy after login

I hope this article will be helpful to everyone in learning javascript and programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template