Jquery增加行和删除行

5211239977d8e3cab86b9d986d33c3fba79.jpg

 1、input自动填充td并去掉边框,实现可编辑

2、序号自动+1

3、选中多选框,点击删除行按钮,删除所选中的行

4、增加行在table尾部追加

html代码如下:

<table class="table" id="table">
 <tr >
  <th>选项</th>
  <th>序号</th>
  <th>车船编号</th>
  <th>油品编号</th>
  <th>油品名称</th>
  <th>数量</th>
  <th>单位</th>
 </tr>
 <tr id="a">
  <td><input type="checkbox" name="test"/></td>
  <td class="td"><input type="text" value="1" readonly style="width: 100%;border:
   0px;outline:none;cursor: pointer;"></td>
  <td><input type="text" style="width: 100%;border: 0px;outline:none;cursor: pointer;" ></td>
  <td><input type="text" style="width: 100%;border: 0px;outline:none;cursor: pointer;" ></td>
  <td><input type="text" style="width: 100%;border: 0px;outline:none;cursor: pointer;" ></td>
  <td><input type="text" style="width: 100%;border: 0px;outline:none;cursor: pointer;" ></td>
  <td><input type="text" style="width: 100%;border: 0px;outline:none;cursor: pointer;" ></td>
 </tr>
</table>
<button onclick="add()" class="btn btn-xs btn-default"><i class="icon-plus">增加行</i></button>
<button onclick="del()" class="btn btn-xs btn-danger"><i class="icon-minus"></i>删除行</button>

 

js代码如下:

function add(){
   var $td = $("#a").clone();       //克隆第一个对象
   $(".table").append($td);         //在table后追加一行
   $(".table tr:last").find("input").val('');   //将尾行元素克隆来的的值清空
   var i = 1;
   $(".td").each(function(){       //增加一行后添加序号1,2,3......
     $(this).find("input").val(i++);
   })
}
function del(){
   $("input[name='test']:checked").each(function() {
     n = $(this).parents("tr").index();
     $("#table").find("tr:eq("+n+")").remove();
   });
}

5、获取table中的值  https://my.oschina.net/u/3502029/blog/2247054

转载于:https://my.oschina.net/u/3502029/blog/2252741

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值