动态添加删除表格2

<script type="text/javascript">
//控制新增图片

var i=2;
var num;
function insRow()
{

 

        x=ttb98.insertRow(ttb98.rows.length);
        x.ln=i;


var num=document.getElementById('num').value;
var h1=x.insertCell(0);
var h2=x.insertCell(1);
h1.innerHTML="";
      
 h2.innerHTML= "新增图片"+(i-1)+"&nbsp;:<input name='pictitle1"+(i)+"' type='text' onfocus=hidethis('pictitle1"+(i)+"') value='图片标题'  id='pictitle1"+(i)+"'>"+"&nbsp;&nbsp;&nbsp;<span style='color:red;'>*</span>test<input   type=button id='delete"+(i)+"'   value= '删除 '   ln= "   +   i   +   "   onclick= 'javascript:delRow(); '> "

if(i==11){alert('最多只能增加十行!');
document.getElementById('picup').style.display='none';
}

var num=document.getElementById('num').value;
num=i;
document.getElementById('num').value=num;
for(j=0;j<11;j++){
if(i>=3){
document.getElementById('delete'+(i-1)+'').style.display='none';
}
}
i=i+1;
x.bgColor="#ffffff"
//alert(i);
}
function   delRow()   //删除一行
{
        line   =   parseInt(event.srcElement.ln,10);

        if   (line> 0){
                for   (j=1;j <ttb98.rows.length;j++) {
                 if   (ttb98.rows[j].ln==line) {
                         ttb98.deleteRow(j);
                   
      i--;
      num=i;
     /*  if(document.getElementById('delete'+(i-1)+'').style.display=''){
       if(i==2){
         alert('删除完成!');
       }else{document.getElementById('delete'+(i-1)+'').style.display=''; }
       }
     */
     
     if(i==2){
         document.getElementById('num').value=1;
      }else{
            num--;
        document.getElementById('num').value=num;
         document.getElementById('delete'+(i-1)+'').style.display='';
           }
      
       
      }
      }
   }
}

 

function hidethis(id){
  document.getElementById(id).value="";
}

 

</script>

<table id="ttb98">
  <tr>
    <td>test page</td>
  </tr>
</table>
<input type="text" value="1" name="num" />
<input id="picup" type='button' value="add" onclick="insRow()" >

你可能感兴趣的:(动态添加删除表格2)