动态生成的html元素绑定click事件

第一篇博客,开启技术博客的生涯,欢迎大家批评指教(坚信妹子也可以做好程序猿)
今天想说帮公司做项目的时候遇到的一个小问题,动态添加html元素以后再去事件监听出问题。在实际开发中会遇到要给动态生成的html元素绑定触发事件的情况。

这里写图片描述

就是上面的一张表格要动态实现添加行,然后序列号还要随着增加,当删除的时候序列号依旧是按顺序排列。
刚开始使用jQueryon方法来解决,但是发现一个问题会出现事件绑定很多次的问题。然后查询了网上的一些资料,使用live进行事件委托,然而还是出现事件绑定多次bug,很崩溃。最后试着用onclick内联,可以完美解决,这里其实有点困惑,但是算是比较完美的解决这个问题。

html:

 <div class="col-sm-12">
           <div class="panel panel-primary">
              <div class="panel-body">
                <div class="block-content collapse in">
                  <div class="span12">
                    <table id="volTab"  class="table">
                          <thead>
                            <tr>
                              <th>ID</th>
                              <th>姓名</th>
                              <th>证件号码</th>
                              <th>手机号码</th>
                              <th>银行卡号</th>
                              <th>E-mail</th>
                              <th>地址</th>
                              <th>删除</th>
                            </tr>
                          </thead>
                          <tbody>
                            <tr id="1">
                            <td>1</td>
                            <td><div><input type="text" class="form-control"/></div></td>
                            <td><div><input type="text" class="form-control"/></div></td>
                            <td><div><input type="text" class="form-control"/></div></td>
                            <td><div><input type="text" class="form-control"/></div></td>
                            <td><div><input type="text" class="form-control"/></div></td>
                            <td><div><input type="text" class="form-control"/></div></td>
                            <td><i class="fa fa-fw fa-minus"></i></td>
                            </tr>
                          </tbody>
                        </table>
                    </div>
                </div>
           </div>
           </div> 
           <button type="button" id="add" style="width:100px;float:left;margin:0 auto;margin-left:40%" class="btn btn-block btn-primary">添加</button>
            <button type="button" id="submit" style="width:100px;float:left;margin:0 auto;margin-left:5px" class="btn btn-block btn-default">提交</button>

js:

<script>
 $("#add").click(function(){
        var len=$("#volTab tr").length;    
        $("#volTab").append("<tr id="+len+">"
                               +"<td>"+len+"</td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><i class='fa fa-fw fa-minus' onclick=\'deltr("+len+")\'"+"'></i></td>"+"</tr>");
     });
  var deltr =function(index)
  {
      debugger;
      var b=$("#volTab tr").length;
      $("#"+index).remove();    
     for(var i=index+1,j=index;i<=b;i++,j++){
                 $("#"+i).replaceWith("<tr id="+(i-1)+">"
                               +"<td>"+(i-1)+"</td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><input type='text' class='form-control'></td>"
                               +"<td><i class='fa fa-fw fa-minus' onclick=\'deltr("+(i-1)+")\'"+"'></i></td>"+"</tr>");

            }
  }
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值