Home > Web Front-end > JS Tutorial > jquery dynamically manipulate table rows

jquery dynamically manipulate table rows

php中世界最好的语言
Release: 2018-04-24 09:51:42
Original
1323 people have browsed it

This time I will bring you jquery dynamic operation table rows. What are the precautions for jquery dynamic operation table rows? The following is a practical case, let’s take a look.

The specific code is as follows:

html code:

<p 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>
 </p>
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 believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

The grep() method implements array filtering

Detailed explanation of the steps for Jquery to parse json string json array (With code)

The above is the detailed content of jquery dynamically manipulate table rows. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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