简体   繁体   English

使用jQuery动态自动增加HTML表行值

[英]Auto Increment HTML table row value dynamically using jQuery

I have an HTML table as follows. 我有一个HTML表,如下所示。

<table id="items">

     <tbody>

      <tr>
          <th>SlNo</th>
          <th>Item</th>

          <th>Unit Cost</th>
          <th>Quantity</th>
          <th>Price</th>
      </tr>


       <tr class="item-row">
         <td class="item-name">
          <div class="delete-wpr"><input type="text" value="1" class="slno"/> <a class="delete" href="javascript:;" title="Remove row">X</a></div></td>

           <td><input type="text" class="slno"/></td>           

          <td><input type="text" class="cost"/></td>
          <td><input type="text" class="qty"/></td>
          <td><span class="price"></span></td>
      </tr>  



    <input type="button" value="submit" onClick="storeAndShowTableValues()"/>


    </tbody>

 <tr id="hiderow">
        <td colspan="5"><a id="addrow" title="Add a row">Add a row</a></td>
        <!-- href="javascript:;" --> 
      </tr>

</table>

The add button perfectly works and all. 添加按钮完美地工作了。 And the Sl:no increases as each row is added. 随着每行的添加,Sl:no增加。 I can also delete the rows too. 我也可以删除行。 But the problem is with numbers. 但是问题出在数字上。 Imagine that there are 7 rows. 想象有7行。 If I delete 6 th row, then the rows should readjust. 如果删除第6行,则应重新调整行。 So I am thinking, whenever a row gets deleted, call some jQuery function, which would reset all the numbers from first row in the incremental order. 所以我在想,每当删除一行时,都调用一些jQuery函数,该函数将按递增顺序重置第一行中的所有数字。 I am using the following functions in jQuery, but it doesn't work. 我在jQuery中使用以下功能,但不起作用。

The following Add row script WORKS PERFECTLY. 以下添加行脚本可以正常工作。

 $("#addrow").click(function(){
index1++;
window.globalCounter++;
$(".item-row:last").after('<tr class="item-row"><td class="item-name"><div class="delete-wpr"><input type="text" value="'+index1+'" class="slno"/><a class="delete" href="javascript:;" title="Remove row">X</a></div></td><td><input type="text" class="slno"/></td><td><input type="text" class="cost"/></td><td><input type="text" class="qty"/></td><td><span class="price"></span></td></tr>');


if ($(".delete").length > 0) $(".delete").show();
bind();

}); });

The delete row function also works, but the script to re-order table indexes doesnt work. 删除行功能也可以,但是重新排序表索引的脚本不起作用。 SO I am thinking about, once the button has been pressed to delete a row, execute a jQuery which removes the selected row at first, and then reset the indexes of all the numbers from 1. 所以我在考虑,一旦按下按钮删除一行,执行一个jQuery,它首先删除选中的行,然后从1重置所有数字的索引。

 $('#items').on('click', ".delete", function(){


     $('#items').find('tr').find('td:first').each(function(index){   //This function doesnt work. 
    $(this).text(index+1);
});

    $(this).parents('.item-row').remove();


    if ($(".delete").length < 2) $(".delete").hide();
});

An easy way would be to forget the number you are using now and set name attributes on the inputs. 一种简单的方法是忘记您现在使用的数字,并在输入中设置名称属性。 If you make them arrays, they will be sent in automatically as a 0-indexed list: 如果将它们制成数组,它们将作为索引为0的列表自动发送:

<input name="slno[]" type="text" class="slno"/>
// etc.

Now you can easily serialize your form, send it in and process it at the backend without having to build the data manually. 现在,您可以轻松地序列化表单,发送表单并在后端对其进行处理,而无需手动构建数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM