-1

我有一张如下表。我必须Amount使用Buy Quantityand字段填充该Market Price字段。Amount = Buy Quantity*Market Price. 我正在做某事——

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Buy Stocks</title>

<script>
(function(){

var table = document.getElementById("mytable");
var tbody = table.getElementsByTagName("tbody")[0];
var rows = tbody.getElementsByTagName("tr");
function populateRow(index, addBlurEvent){
    var row = rows[index];
var cells = row.getElementsByTagName("td")
    var textboxes = row.getElementsByTagName("input");
var amountTextbox = textboxes[0];
    var totalTextbox = textboxes[1];
    var costCell = cells[2];
    var amount = amountTextbox.value.length>0 ? parseFloat(amountTextbox.value) : 0;
    var cost = parseFloat(costCell.innerHTML);
var total = amount * cost;
    totalTextbox.value = total;
if (addBlurEvent) {
        amountTextbox.onblur = function(){ populateRow(index,false); };
    }
}
for(i=0;i<rows.length;i++){
    populateRow(i,true);    
}
})();


</script>
</head>
<body>
<center>

<h5>Buy Stocks Here</h5>
    <form >
   <table border="1" cellpadding="5" id="mytable">
    <thead>
      <tr>
        <th>Stock Name</th>
        <th>Buy Quantity</th>
        <th>Market Price</th>
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
<tr>
        <td>Stock Name</td>
        <td><input type="text" name="quantity" ></td>
        <td>122</td>
        <td><input type="text" name="amount"></td>
      </tr>

      <tr>
        <td>Stock Name</td>
        <td><input type="text" name="quantity" ></td>
        <td>111</td>
        <td><input type="text" name="amount"></td>
      </tr>

    </tbody>
  </table>
 </form>
</center>

上面的 html 的 javascript 函数应该可以正常工作,我无法处理 'onblur' 事件。我应该如何以及在哪里这样做?请提出一个不修改脚本的答案。谢谢。

图片供参考:

在此处输入图像描述

4

1 回答 1

4

看到这个:http: //jsfiddle.net/picklespy/cxUz9/

如果您只想使用您的代码而不是我上面给出的 jquery 代码,请使用以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Buy Stocks</title>
</head>
<body>
<center>

<h5>Buy Stocks Here</h5>
    <form >
   <table border="1" cellpadding="5" id="mytable">
    <thead>
      <tr>
        <th>Stock Name</th>
        <th>Buy Quantity</th>
        <th>Market Price</th>
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
<tr>
        <td>Stock Name</td>
        <td><input type="text" name="quantity" ></td>
        <td>122</td>
        <td><input type="text" name="amount"></td>
      </tr>

      <tr>
        <td>Stock Name</td>
        <td><input type="text" name="quantity" ></td>
        <td>111</td>
        <td><input type="text" name="amount"></td>
      </tr>

    </tbody>
  </table>
 </form>
</center>
<script type="text/javascript">
(function(){

var table = document.getElementById("mytable");
var tbody = table.getElementsByTagName("tbody")[0];
var rows = tbody.getElementsByTagName("tr");
function populateRow(index, addBlurEvent){
    var row = rows[index];
var cells = row.getElementsByTagName("td")
    var textboxes = row.getElementsByTagName("input");
var amountTextbox = textboxes[0];
    var totalTextbox = textboxes[1];
    var costCell = cells[2];
    var amount = amountTextbox.value.length>0 ? parseFloat(amountTextbox.value) : 0;
    var cost = parseFloat(costCell.innerHTML);
var total = amount * cost;
    totalTextbox.value = total;
if (addBlurEvent) {
        amountTextbox.onblur = function(){ populateRow(index,false); };
    }
}
for(i=0;i<rows.length;i++){
    populateRow(i,true);    
}
})();
</script>
</body>
</html>
于 2012-09-27T06:04:43.873 回答