你要明白,你构造的table的结构,你这里连结构都没有弄对呢。首先,你点击的时候,是点击的一个input标签,这个input标签的父节点是td标签,可是这里你是想要获取tr标签吧?所以,首先你获取行数的时候,就是错误的,
var i=r.parentNode.parentNode.rowIndex;
要取父节点的父节点,才是tr标签。然后是你进行input取值时,
document.getElementById('myTable').rows[i].cells[1].innerHTML
看下面这部分,你是获取到的对象时:
document.getElementById('myTable').rows[i].cells[1]
这是一个td标签,如果使用innerHTML进行对这个td标签取值,那么取值的结果只是对应的input标签,就像:
<input type="text" name="txtPrice"> 
这样还是没有获取到对应的值啊。错误呢,就是这样,自己试着修改吧。

解决方案 »

  1.   

    function mult(r)
    {
    var i=r.parentNode.parentNode.rowIndex;
    var price,discount,quantity,total;
    price=parseFloat(document.getElementById('myTable').rows[i].getElementsByTagName("input")[0].value);
    discount=parseFloat(document.getElementById('myTable').rows[i].getElementsByTagName("input")[1].value);
    quantity=parseInt(document.getElementById('myTable').rows[i].getElementsByTagName("input")[2].value);
    total= price*discount*quantity;
    document.getElementById('myTable').rows[i].getElementsByTagName("input")[4].value=total;
    }
      

  2.   

    我不是已经给你改好了么?你怎么试的?<html>
    <head>
    <script language="JavaScript">
    function mult(r)
    {
    var i=r.parentNode.parentNode.rowIndex;
    var price,discount,quantity,total;
    price=parseFloat(document.getElementById('myTable').rows[i].getElementsByTagName("input")[0].value);
    discount=parseFloat(document.getElementById('myTable').rows[i].getElementsByTagName("input")[1].value);
    quantity=parseInt(document.getElementById('myTable').rows[i].getElementsByTagName("input")[2].value);
    total= price*discount*quantity;
    document.getElementById('myTable').rows[i].getElementsByTagName("input")[4].value=total;
    }
    </script>
    </head>
    <BODY>
    <center><h1>BookZone Store</h1></center>
    <TABLE id="myTable" BORDER=1 align=center width="70%">
    <TR><TD>BookName</TD><TD>Price$</TD><TD>Discount</TD><TD>Quantity</TD><TD>Calculate</TD><TD>Total Sale($)</TD></TR><TR><TD>Greatwinner</TD><TD><input type="text" name="txtPrice"></TD><TD><input type="text" name="txtDiscount"></TD><TD><input type="text" name="txtQuantity"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text" name="txtTotal"></TD></TR><TR><TD>the scholar</TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text"></TD></TR><TR><TD>the scholar</TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text"></TD></TR><TR><TD>the scholar</TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text"></TD></TR></TABLE>
    </BODY>
    </html>
      

  3.   


    <html>
    <head>
    <script type="text/javascript">
    function DRow(r)
    {
      var i=r.parentNode.rowIndex
      document.getElementById('myTable').deleteRow(i)
    }
    </script>
    </head>
    <body><table id="myTable" border="1">
    <tr>
      <td>Row 1</td>
      <td><input type="button" value="删除" onclick="DRow(this)"></td>
    </tr>
    <tr>
      <td>Row 2</td>
      <td><input type="button" value="删除" onclick="DRow(this)"></td>
    </tr>
    <tr>
      <td>Row 3</td>
      <td><input type="button" value="删除" onclick="DRow(this)"></td>
    </tr>
    </table></body>
    </html>
    为什么要两个parentNode啊,这个例子只用了一个,也能运行哎
      

  4.   

    你那个this根本就没办法把行号传给那个方法,建议你用我这个<html>
    <head>
    <script language="JavaScript">
    function mult(r)
    {
    var i=r.parentNode.rowIndex;
    var price,discount,quantity,total;
    price=parseFloat(document.getElementById('txtPrice').value);
    discount=parseFloat(document.getElementById('txtDiscount').value);
    quantity=parseFloat(document.getElementById('txtQuantity').value);
    total= price*discount*quantity;document.getElementById('txtTotal').value=total;
    }
    </script></head>
    <BODY>
    <center><h1>BookZone Store</h1></center>
    <TABLE id="myTable" BORDER=1 align=center width="70%">
    <TR><TD>BookName</TD><TD>Price$</TD><TD>Discount</TD><TD>Quantity</TD><TD>Calculate</TD><TD>Total Sale($)</TD></TR><TR><TD>Greatwinner</TD><TD><input type="text" id="txtPrice"></TD><TD><input type="text" id="txtDiscount"></TD><TD><input type="text" id="txtQuantity"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text" id="txtTotal"></TD></TR><TR><TD>the scholar</TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text"></TD></TR><TR><TD>the scholar</TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text"></TD></TR><TR><TD>the scholar</TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="text"></TD><TD><input type="button" value="Calculate" onclick="mult(this)"></TD><TD><input type="text"></TD></TR></TABLE>
    </BODY>
    </html>