新增 数据行 -jquery

来源:互联网 发布:服务器性能监测软件 编辑:程序博客网 时间:2024/05/16 12:43
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>

<body>
<script id="li-list" type="text/x-jquery-tmpl">
  <li>
      <label>${num}.</label>
      姓名:<input type="text" value="" name="name[${num}]" />&nbsp;
      年龄:<input type="text" value=""  name="age[${num}]"/>&nbsp;
      <button _val='del'><img src="images/del.png" /></button>
      <button _val='add'><img src="images/plus.png" /></button>
  </li>
</script>
<ul id="myul">
 
</ul>
</body>
</html>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="js/jquery.tmpl.js"></script>
<script type="text/javascript">
  var num=1;
  function add(){
      $("#li-list").tmpl({num:$("ul>li").length}).appendTo("#myul");
      $('ul>li:not(:last)>button').filter("[_val='add']").hide();
      $("ul li:last button").filter("[_avl='add']").show();
      $("ul li:not(:last) button").filter("[_val='del']").show();
      $("ul li:last button").filter("[_val='del']").hide();  
  }
  $(document).ready(function(){
      $("ul>li>button").live("click",function(){
         if($(this).attr('_val')=='add'){
             add();
         }else{
             $(this).parent().remove();
         }
      });
     add();
  });
</script>