谁给做一个
点击增加时增加
点击删除时,删除对应的行就行

解决方案 »

  1.   

    http://hi.csdn.net/attachment/200910/14/3010852_12555268372Box.jpg
      

  2.   

    http://www.laoney.net/post/2008/12/02/200812022221176378.aspx
      

  3.   


    <!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=gb2312" /> 
    <title></title> 
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js "> </script>
    </head> 
    <body>
    <input type="button" value="添加" onclick="addu();" /> 
    <table id="T_model"> ds
    <table> 
    <div id="showbutton" style="display:'';"> 
    <input id="SubTT" type="button" value="提交"  onclick="btnOK();"/> 
     </div> 
    <script language="javascript">
    function addu() { 
        var tb=document.getElementById("T_model");//
        var tr=tb.insertRow(tb.rows.length);//添加行
        for(var i=0;i<3;i++) {
            var td=tr.insertCell(i);//添加单元格
            td.innerHTML="<input type='text'/>";//往单元格里插入html或者$(td).html("<input type='text'/>");
        }
        var lasttd=tr.insertCell(tr.cells.length);//插入删除那个单元格
        lasttd.innerHTML="<input type='button' onclick='btndel(this)' value='Delete'/>";

    function btnOK() { 
        var aa=""; 
        $("#T_model input[type='text']").each(function(){ //只取type='text'的
            aa+=$(this).val()+"|";
        }) 
        alert(aa)
        return aa; //这里没必要返回

    function btndel(obj) {
        var tr=obj.parentNode.parentNode;//获取 当前行
        tr.parentNode.removeChild(tr);
    } </script>
    </body> 
    </html> 
      

  4.   

    <input type="button" value="新增一个航程" onclick="create()" />
    <br />
    <table id="table">
    <tr><th>起飞时间</th><th>抵达时间</th><th>舱位</th></tr>
    </table><script>
    function create()
    {
    var table = document.getElementById("table");
    var tbody = document.createElement("tbody"); 
    var tr = document.createElement("tr");
    for(var i = 0; i < 3; i++) 
    {
    var td = document.createElement("td");
    tr.appendChild(td);
    td.innerHTML = "<input />";
    }
    var td = document.createElement("td");
    tr.appendChild(td);
    td.innerHTML = "<input type='button' value='删除' onclick='remove(this)' />"
    tbody.appendChild(tr);
    table.appendChild(tbody);
    }function remove(input)
    {
    var tr = input.parentNode.parentNode;
    tr.parentNode.removeChild(tr);
    }
    </script>
      

  5.   

    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    <style type="text/css">*{font-size:14px}button{margin:3px} </style> 
    <script type="text/javascript"> var mytable=null; window.onload=function(){ 
    mytable=new CTable("tbl",10); 
    } Array.prototype.each=function(f){for(var i=0;i <this.length;i++) f(this[i],i,this)} function $A(arrayLike){ 
    for(var i=0,ret=[];i <arrayLike.length;i++) ret.push(arrayLike[i]); 
    return ret 
    } Function.prototype.bind = function() { 
      var __method = this, args = $A(arguments), object = args.shift(); 
      return function() { 
        return __method.apply(object, args.concat($A(arguments))); 
      } 

    function CTable(id,rows){ 
    this.tbl=typeof(id)=="string"?document.getElementById(id):id;  
    if (rows && /^\d+$/.test(rows)) this.addrows(rows) 
    } CTable.prototype={ 
    addrows:function(n){          //随机添加n个tr 
    new Array(n).each(this.add.bind(this)) 
    }, 
    add:function(){          //添加1个tr 
    var self=this; 
    var tr = self.tbl.insertRow(-1),td1= tr.insertCell(-1),td2= tr.insertCell(-1); 
    var chkbox=document.createElement("INPUT") 
    chkbox.type="checkbox" 
    chkbox.onclick=self.highlight.bind(self) 
    td1.appendChild(chkbox) 
    td1.setAttribute("width","35") 
    td2.innerHTML="A: <INPUT NAME=A /> <br />B: <INPUT NAME=B />"; 
    }, 
    del:function(){          //删除所选tr 
    var self=this 
    $A(self.tbl.rows).each(function(tr){if (self.getChkBox(tr).checked) tr.parentNode.removeChild(tr)}) 
    }, 
    up:function(){              //上移所选tr 
    var self=this 
    var upOne=function(tr){          //上移1个tr 
    if (tr.rowIndex>0){ 
    self.swapTr(tr,self.tbl.rows[tr.rowIndex-1]) 
    self.getChkBox(tr).checked=true 


    var arr=$A(self.tbl.rows).reverse() 
    if (arr.length>0 && self.getChkBox(arr[arr.length-1]).checked){ 
    for(var i=arr.length-1;i>=0;i--){ 
    if (self.getChkBox(arr[i]).checked){ 
    arr.pop()            
    }else{ 
    break 



    arr.reverse().each(function(tr){if (self.getChkBox(tr).checked) upOne(tr)}); 
    }, 
    down:function(){ 
    var self=this 
    var downOne=function(tr){      
    if (tr.rowIndex <self.tbl.rows.length-1)  { 
    self.swapTr(tr,self.tbl.rows[tr.rowIndex+1]); 
    self.getChkBox(tr).checked=true; 


    var arr=$A(self.tbl.rows) if (arr.length>0 && self.getChkBox(arr[arr.length-1]).checked){ 
    for(var i=arr.length-1;i>=0;i--){ 
    if (self.getChkBox(arr[i]).checked){ 
    arr.pop() 
    }else{ 
    break 



    arr.reverse().each(function(tr){if (self.getChkBox(tr).checked) downOne(tr)}); 
    }, 
    highlight:function(){              //设置tr的背景色 
    var self=this; 
    var evt=arguments[0] || window.event 
    var chkbox=evt.srcElement || evt.target 
    var tr=chkbox.parentNode.parentNode 
    chkbox.checked?self.setBgColor(tr):self.restoreBgColor(tr) 
    }, 
    swapTr:function(tr1,tr2){            //交换tr1和tr2的位置 
    var target=(tr1.rowIndex <tr2.rowIndex)?tr2.nextSibling:tr2; 
    var tBody=tr1.parentNode 
    tBody.replaceChild(tr2,tr1); 
        tBody.insertBefore(tr1,target); 
    }, 
    getChkBox:function(tr){          //从tr得到 checkbox对象 
    return tr.cells[0].firstChild 
    }, 
    restoreBgColor:function(tr){          
    tr.style.backgroundColor="#ffffff"  
    }, 
    setBgColor:function(tr){ 
    tr.style.backgroundColor="#c0c0c0" 

    } function f(a,b){ 
    var sumRow=function(row){return Number(row.cells[1].innerHTML)+Number(row.cells[2].innerHTML)}; 
    return sumRow(a)>sumRow(b)?1:-1; 
    } </script> </head> 
    <body> <button  onClick="javascript:mytable.add()">添加一行 </button> 
    <button  onClick="javascript:mytable.del()">删除选定行 </button> 
    <button  onClick="javascript:mytable.up()">上移选定行 </button> 
    <button  onClick="javascript:mytable.down()">下移选定行 </button> 
    <table width=100%> 
    <tr> 
    <td valign="top"> <table border id="tbl" width="80%"> </table> </td> 
    </tr> 
    </table> 
    </body> 
    </html> 
      

  6.   

    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    <style type="text/css">*{font-size:14px}button{margin:3px} </style> 
    <script type="text/javascript"> var mytable=null; window.onload=function(){ 
    mytable=new CTable("tbl",10); 
    } Array.prototype.each=function(f){for(var i=0;i <this.length;i++) f(this[i],i,this)} function $A(arrayLike){ 
    for(var i=0,ret=[];i <arrayLike.length;i++) ret.push(arrayLike[i]); 
    return ret 
    } Function.prototype.bind = function() { 
      var __method = this, args = $A(arguments), object = args.shift(); 
      return function() { 
        return __method.apply(object, args.concat($A(arguments))); 
      } 

    function CTable(id,rows){ 
    this.tbl=typeof(id)=="string"?document.getElementById(id):id;  
    if (rows && /^\d+$/.test(rows)) this.addrows(rows) 
    } CTable.prototype={ 
    addrows:function(n){          //随机添加n个tr 
    new Array(n).each(this.add.bind(this)) 
    }, 
    add:function(){          //添加1个tr 
    var self=this; 
    var tr = self.tbl.insertRow(-1),td1= tr.insertCell(-1),td2= tr.insertCell(-1); 
    var chkbox=document.createElement("INPUT") 
    chkbox.type="checkbox" 
    chkbox.onclick=self.highlight.bind(self) 
    td1.appendChild(chkbox) 
    td1.setAttribute("width","35") 
    td2.innerHTML="A: <INPUT NAME=A /> <br />B: <INPUT NAME=B />"; 
    }, 
    del:function(){          //删除所选tr 
    var self=this 
    $A(self.tbl.rows).each(function(tr){if (self.getChkBox(tr).checked) tr.parentNode.removeChild(tr)}) 
    }, 
    up:function(){              //上移所选tr 
    var self=this 
    var upOne=function(tr){          //上移1个tr 
    if (tr.rowIndex>0){ 
    self.swapTr(tr,self.tbl.rows[tr.rowIndex-1]) 
    self.getChkBox(tr).checked=true 


    var arr=$A(self.tbl.rows).reverse() 
    if (arr.length>0 && self.getChkBox(arr[arr.length-1]).checked){ 
    for(var i=arr.length-1;i>=0;i--){ 
    if (self.getChkBox(arr[i]).checked){ 
    arr.pop()            
    }else{ 
    break 



    arr.reverse().each(function(tr){if (self.getChkBox(tr).checked) upOne(tr)}); 
    }, 
    down:function(){ 
    var self=this 
    var downOne=function(tr){      
    if (tr.rowIndex <self.tbl.rows.length-1)  { 
    self.swapTr(tr,self.tbl.rows[tr.rowIndex+1]); 
    self.getChkBox(tr).checked=true; 


    var arr=$A(self.tbl.rows) if (arr.length>0 && self.getChkBox(arr[arr.length-1]).checked){ 
    for(var i=arr.length-1;i>=0;i--){ 
    if (self.getChkBox(arr[i]).checked){ 
    arr.pop() 
    }else{ 
    break 



    arr.reverse().each(function(tr){if (self.getChkBox(tr).checked) downOne(tr)}); 
    }, 
    highlight:function(){              //设置tr的背景色 
    var self=this; 
    var evt=arguments[0] || window.event 
    var chkbox=evt.srcElement || evt.target 
    var tr=chkbox.parentNode.parentNode 
    chkbox.checked?self.setBgColor(tr):self.restoreBgColor(tr) 
    }, 
    swapTr:function(tr1,tr2){            //交换tr1和tr2的位置 
    var target=(tr1.rowIndex <tr2.rowIndex)?tr2.nextSibling:tr2; 
    var tBody=tr1.parentNode 
    tBody.replaceChild(tr2,tr1); 
        tBody.insertBefore(tr1,target); 
    }, 
    getChkBox:function(tr){          //从tr得到 checkbox对象 
    return tr.cells[0].firstChild 
    }, 
    restoreBgColor:function(tr){          
    tr.style.backgroundColor="#ffffff"  
    }, 
    setBgColor:function(tr){ 
    tr.style.backgroundColor="#c0c0c0" 

    } function f(a,b){ 
    var sumRow=function(row){return Number(row.cells[1].innerHTML)+Number(row.cells[2].innerHTML)}; 
    return sumRow(a)>sumRow(b)?1:-1; 
    } </script> </head> 
    <body> <button  onClick="javascript:mytable.add()">添加一行 </button> 
    <button  onClick="javascript:mytable.del()">删除选定行 </button> 
    <button  onClick="javascript:mytable.up()">上移选定行 </button> 
    <button  onClick="javascript:mytable.down()">下移选定行 </button> 
    <table width=100%> 
    <tr> 
    <td valign="top"> <table border id="tbl" width="80%"> </table> </td> 
    </tr> 
    </table> 
    </body> 
    </html> 
      

  7.   

    从tr里可有后去任意一个td
    可为什么从table里获取任意一个tr就不可以
    请问怎么获取
      

  8.   

    从tr里可以获取任意一个td 
    可为什么从table里获取任意一个tr就不可以 
    请问怎么获取