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

javascript删除表格中的一行
今天写购物车的时候,学了下js如何动态删除一行,
例子中是删除第二行。
代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script language="javascript">

/*删除行,采用deleteRow(row Index)*/
function removeRow(){
  /* var row=document.getElementById("2");
   var index=row.rowIndex;
   alert(index);*/
   alert(document.getElementById("r2").rowIndex);
   document.getElementById("t1").deleteRow(document.getElementById("r2").rowIndex);
}

</script>

<table width="200" border="1" id="t1">
  <tr id="r1">
    <td>1</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr id="r2">
    <td>2</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr id="r3">
    <td>3</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr id="r4">
    <td>4</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr id="r5">
    <td>5</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<p>
  <label>
  <input type="button" name="Submit" value="按钮" onclick="removeRow()"/>
  </label>
</p>
</body>
</html>
后来是根据checkbox是否选择来删除。再使用ajax更新session中的产品信息,