jq实现表格之间的删除移动

公司的项目要求给后台系统添加一下功能,将公司的成员选择并移动到其他部门,想了挺久做了一个demo出来,其中涉及全选和remove;代码如下;


项目引用了jq

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head runat="server">
<title>jQuery实现CheckBox全选、全不选</title>
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
        $(function() {
            // 全选
           $("#checkAll").click(function() {
                $(this).next().find("input").attr("checked",this.checked); 
            });
            var $subBox = $("#checkAll").next().find("input");
            $subBox.click(function(){
                $("#checkAll").attr("checked",$subBox.length == $("$subBox:checked").length ? true : false);
            });
            // 删除选中
            var move =document.querySelectorAll('.move button');
            console.log(move.length)
            for (var i = 0;i<move.length;i++) {
                (function(i){
                  move[i].onclick=function(){
                      var moveTo = $(".moveTo");
                      $('input[name="test"]').each(function(no) {    
                       if($(this).attr('type')=='checkbox'){ 
                         if($(this).attr('checked')){
                           $(this).parent().parent().remove();
                           $(this).removeAttr("checked").parent().parent().appendTo(moveTo.eq(i).find("div table"));
                         }
                       };
                    });

                  }
                })(i)

            }
// 分别点击事件

    //         $("button.move2").click(function() {
    //          $('input[name="test"]').each(function(no) {    
    //        if($(this).attr('type')=='checkbox'){ 
    //          if($(this).attr('checked')){
    //            $(this).parent().parent().remove();
    //            $(this).removeAttr("checked").parent().parent().appendTo(".table2 table");
    //          }
    //        };
    //     });
    // });



    //         $("button.move3").click(function() {
    //          $('input[name="test"]').each(function(no) {    
    //        if($(this).attr('type')=='checkbox'){ 
    //          if($(this).attr('checked')){
    //            $(this).parent().parent().remove();
    //            $(this).removeAttr("checked").parent().parent().appendTo(".table3 table");
    //          }
    //        };
    //     });
    // });




    //         $("button.move4").click(function() {
    //          $('input[name="test"]').each(function(no) {    
    //        if($(this).attr('type')=='checkbox'){ 
    //          if($(this).attr('checked')){
    //            $(this).parent().parent().remove();
    //            $(this).removeAttr("checked").parent().parent().appendTo(".table4 table");
    //          }
    //        };
    //     });
    // });

// 分别点击事件

        });
    </script>

</head>
<body>

    <div class="table1">
        <input id="checkAll" type="checkbox" />全选
        <table id = "test_table" border="1">
            <tr><td><input type="checkbox" name="test" /></td><td>1</td><td>2</td><td>3</td></tr>
            <tr><td><input type="checkbox" name="test" /></td><td>4</td><td>5</td><td>6</td></tr>
            <tr><td><input type="checkbox" name="test" /></td><td>7</td><td>8</td><td>9</td></tr>
            <tr><td><input type="checkbox" name="test" /></td><td>10</td><td>11</td><td>12</td></tr>
        </table>
        <div class="move">
          <button class="move2">移动到2</button>
          <button class="move3">移动到3</button>
          <button class="move4">移动到4</button>
        </div>

    </div>
    </br></br>
    <div class="moveTo">
    <p>222222222</p>
      <div class="table2">
        <table border="1">
            <tr><td><input name="test" type="checkbox" /></td><td>1</td><td>2</td><td>3</td></tr>
            <tr><td><input name="test" type="checkbox" /></td><td>4</td><td>5</td><td>6</td></tr>
        </table>
    </div>
    </div>

    </br></br>
    <div class="moveTo">
      <p>33333333333</p>
      <div class="table3">
        <table border="1">
            <tr><td><input name="test" type="checkbox" /></td><td>1</td><td>2</td><td>3</td></tr>
            <tr><td><input name="test" type="checkbox" /></td><td>4</td><td>5</td><td>6</td></tr>
        </table>
    </div>
    </div>

    </br></br>
    <div class="moveTo">
      <p>444444444444</p>
      <div class="table4">
        <table border="1">
            <tr><td><input name="test" type="checkbox" /></td><td>1</td><td>2</td><td>3</td></tr>
            <tr><td><input name="test" type="checkbox" /></td><td>4</td><td>5</td><td>6</td></tr>
        </table>
    </div>
    </div>

</body>
</html>

遇到的问题

  1. 一开始遇到的瓶颈是选择行并移动到其他表格,发现checkbox的状态还是选中的,我就在移动之前removeAttr,怎么移都一不掉,后来发现是this的指向不对,修改过后就可以了。
  2. 最开始button的点击事件我是单独一个个给的,后来想想可以优化一下,可以遍历添加点击事件,并根据i判断要移动到的表格;一个for循环添加点击事件怎么都添加不了,后来发现i被污染了,始终是最大值;所以我用自执行函数解决了。

大概就这样,有什么问题欢迎一起讨论,谢谢~!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值