日期:2014-05-16  浏览次数:20323 次

使用jquery给表格添加或删除行
想实现下面的功能
图我看一下怎么传吧!试了半天没成功
------解决方案--------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>

<body>
<table border="1" id="table">
<tr>
  <td rowspan="2" class="kh">项目</th>
  <td>item1</td>
 <td rowspan="2" class="kh">列表</th>
  <td>item2</td>
</tr>
<tr class="dh">
  <td >list1</td>
  <td>list2</td>
</tr>
</table>
<button id="add">添加</button>
<button id='remove'>删除</button>

<script language="JavaScript">
    $(document).ready(function() {
      var kh = $("#table").find('.kh');//跨列
      var dh = $("#table").find('.dh');//多列
      var rowspan = $(kh).prop('rowspan');
      $("#add").click(function(){
        $(kh).prop('rowspan',++rowspan);
        $(dh).after('<tr><td>item1</td><td>item1</td></tr>')
      })
      $("#remove").click(function(){
        
        if(rowspan > 2){
          $(kh).prop('rowspan',--rowspan);
          $(dh).siblings(':last').remove();
        }
        
      })
    });
  </script>
</body>
</html>

------解决方案--------------------
你可以采取表格套表格的形式来实现合并

<!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>无标题文档</title>
<style>
table{margin:0;padding:0;border-collapse:collapse;}
td{border:solid 1px #ccc;}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function(){
var i=0;
$("#add").click(function(){
$("#list1").append('<tr><td>item'+i+'</td></tr>');
$("#list2").append('<tr><td>列表'+i+'</td></tr>');
i++;

});
$("#del").click(function(){
$("#list1 tr:last").remove();
$("#list2 tr:last").remove();
i++;

});
})
</script>

</head>

<body>
<table id="mT">
<tr>
     <td>项目</td>
        <td>
         <table id=