html中keypress/keydown/keyup事件的计算


calculation on keypress/keydown/keyup event in html

我在一个表单上有25行,供用户在(输入标签)中输入项目代码,并在java脚本中根据该项目代码计算该项目数量的值,并以金额(输入标记)显示
onKeypress/up/down中触发的事件跟随

function qtycal() {
    for (i = 1; i <= 25; i++) {
        var quantity_j = document.getElementById("quantity_" + i);
        var price_j = document.getElementById("price_" + i);
        var amount_j = document.getElementById("amount_" + i);
        amount_j.value = price_j.value * quantity_j.value;

    } // end of for

正如在按键/向下/向上上发射的一样

问题是当我输入第一个数字时,它不计算值并以金额显示,在输入第二个数字后,事件再次触发,但不是用price_j的quantity_j的多整数值

如何使事件给出确切的金额。

html代码

    <table border="1"  align="center">
<thead>
<tr>
<th>#</th>
<th>Item Name</th>
<th>Item Code</th>
<!--<th>Stock</th>-->
<th>Quantity</th>
<th>Price</th>
<!--<th>Discount %</th>
<th>Amount</th>-->
<!--<th>Discount Amount</th>-->
<th>Net Amount</th>
</tr>
</thead>
<tbody>
<?php for($i=1 ; $i<=25 ; $i++) { ?>
<tr>
<th> <?php echo "$i"; ?> </th>
<td><input id="itemName_<?php echo $i; ?>"  onBlur="test()" class="orderInput" type="text" align="center"  ></td>
<td><input id="itemCode_<?php echo $i; ?>"   onBlur="loaditem()"  class="orderInput" type="text" align="center" ></td>
<td><input id="quantity_<?php echo $i; ?>"    onBlur="qtycal()" class="orderInput"  type="text" align="center" ></td>
<td><input id="price_<?php echo $i; ?>" class="orderInput" type="text" align="center"  readonly></td>
<td><input id="amount_<?php echo $i; ?>" class="orderInput" type="text" align="center" readonly ></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td id="tdTotal" colspan="2" style="font-size:16px"> <b>Totals</b></td>
<td id="totalspace" colspan="3"> </td>
<td><input id="netTotalAll" class="orderInput" type="text" readonly> </td>
</tr>
<button id="addBtn"  ><img src="images/add.png" align="middle"></button>
</tfoot>
</table>

AFAIK您必须将函数绑定到keyup才能获得实际值。

如果这不起作用,您可以使用setTimeout("qtycal",100)对函数进行一点延迟。它不会被您的用户注意到,并将使您的计算工作。

看起来你计算得很好,但amount_j不是对DOM值的引用,所以试试这个:

var quantity_j = document.getElementById("quantity_" + i).value;
var price_j = document.getElementById("price_" + i).value;
var amount_j = document.getElementById("amount_" + i).value;
amount_j = price_j * quantity_j;
document.getElementById("amount_" + i).value=amount_j;
// @see http://jsfiddle.net/scafa/GFsPY/
$(document).ready(function(){
  // creating some sample data rows
  var tb = $("<tbody>");
  for(var i=1;i<=10;i++){
    var row = $("<tr>");
    row.append($("<td>").html(i));
    row.append($("<td>").html("A"+i));
     row.append($("<td>").html($("<input>").attr("size",8).val(Math.round(Math.random()*10))));    
     row.append($("<td>").html($("<input>").attr("size",8).val(Math.round(Math.random()*100))));    
     row.append($("<td>").append($("<input>")));
     tb.append(row);   
  }
  $("table").append(tb);
  // calculating
  $("table input").bind("blur", function(){
    var total = 0;
    $.each($("table tbody tr"), function(i, r){
      var fields = $(r).find("input");
      $(fields[2]).val($(fields[0]).val() * $(fields[1]).val());
      total += parseInt($(fields[2]).val());
    });
    $(".total").val(total);
  });
});

虽然没有PHP环境,但我只是写了一个小提琴。也许这会给你一个如何解决问题的提示。别怪我,我几分钟后就黑了。但它是有效的。这只是一个例子!