jquery实现table动态添加行、删除行以及行的上移和下移

当前元素.click(function(){}); 和 当前元素.on(‘click’,function(){});这两种写法是一样的;还可以写成 $(document).on(‘click’,’当前元素’,function(){});

//js代码
$(function(){
    $('#insertRow').click(function(){
        var $tr = $('#templateTr').clone(true);
        $tr.attr('id','');
        $('#columnid tbody').append($tr);
        $tr.show();
    });

    $('#columnid .delrow').click(function(){
        var $tr = $(this).parents("tr");
        $tr.remove();
    });

    $('#columnid .moveup').on('click',function(){
        $(this).each(function(){
            var $tr = $(this).parents("tr");
            if($tr.index() != 0){
                $tr.prev().before($tr);
            }
        });
    });

    $('#columnid .movedown').on('click',function(){
        var trLength = $(this).length;
        $(this).each(function(){
            var $tr = $(this).parents("tr"); 
            if ($tr.index() != trLength - 1) { 
                $tr.next().after($tr);
            }
        });
    });

    /*$(document).on('click','#columnid .movedown',function(){
        var trLength = $("#columnid .movedown").length;
        $(this).each(function(){
            var $tr = $(this).parents("tr"); 
            if ($tr.index() != trLength - 1) { 
                $tr.next().after($tr);
            }
        });
    });*/
});
//html页面代码,这里的很多class属性值都是bootstrap框架的样式,可以忽略
<body>
<h3 style="margin-left: 1%">新增任务</h3>
<form class="form-horizontal" role="form" action="">
  <div class="form-group">
    <label class="col-sm-3 control-label">任务名称</label>
    <div class="col-sm-3">
      <input type="text" class="form-control input-sm" id="" placeholder="请输入任务名称" />
    </div>
    <label class="col-sm-1 control-label">数据源</label>
    <div class="col-sm-3">
      <select class="form-control input-sm">
        <option>选项</option>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-3 control-label">schema</label>
    <div class="col-sm-3">
      <input type="text" class="form-control input-sm" id="" />
    </div>
    <label class="col-sm-1 control-label">周期</label>
    <div class="col-sm-3">
      <select class="form-control input-sm">
        <option value="1"></option>
        <option value="2"></option>
        <option value="3"></option>
        <option value="4">季度</option>
      </select>
    </div>
  </div>
  <a class="btn btn-primary btn-sm" id="insertRow" href="javascript:void(0);">
      <span class="glyphicon glyphicon-plus"></span> 新增</a>
  <table id="columnid" class="table table-striped table-bordered table-hover table-condensed">
    <thead>
      <tr id="trcolumn">
        <th>字段名称</th>
        <th class="col-sm-1">字段类型</th>
        <th>属性</th>
        <th>参与主键</th>
        <th class="col-sm-2">操作</th>
      </tr>
    </thead>
    <tbody>
      <tr id="templateTr" style="display: none;">
        <td><input type="text" class="form-control input-sm" placeholder="请输入字段名称"/></td>
        <td>
          <select class="form-control input-sm">
            <option value="1">数字</option>
            <option value="2">字符</option>
            <option value="3">日期</option>
          </select>
        </td>
        <td><input type="text" class="form-control input-sm" placeholder="如果是字符,填字符长度;如果是日期,填日期格式"/></td>
        <td><label class="checkbox-inline"><input type="checkbox" name="bePk" />参与主键</label></td>       
        <td>
          <a class="btn btn-primary btn-xs delrow" href="javacript:void(0);"><span class="glyphicon glyphicon-trash"></span> 删除</a>
          <a class="btn btn-primary btn-xs moveup" href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-up"></span> 上移</a>
          <a class="btn btn-primary btn-xs movedown" href="javascript:void(0);"><span class="glyphicon glyphicon-arrow-down"></span> 下移</a>
        </td>
      </tr>
    </tbody>
  </table>
  <div class="form-group">
  <label class="col-sm-5 control-label"></label>
    <div class="col-sm-2">
      <button type="submit" class="btn btn-primary">确定</button>
      <button type="button" class="btn btn-primary" onclick="history.go(-1);">取消</button>
    </div>
  </div>
</form>
</body>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值