doulai8405 2013-06-26 12:52
浏览 73

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

i have 25 rows on a form for user entering the item code in (input tag) and qty (input tag) against that item code in java script calculate the value of that item's quantity and show them in amount (input tag)
The Event fired in the onKeypress/up/down is following

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

as in this fired on keypress/down/up

the problem is when i typed first digit it not calculate the value and show in amount and after entering second digit event again fired but not multyply whole value of quantity_j with price_j

How to make event so it give exact amount.

The html code

    <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>
  • 写回答

3条回答 默认 最新

  • drhzn3911 2013-06-26 12:58
    关注

    AFAIK you have to bind your function to the keyup to get the actual value.

    If that doesn't work you can put a little delay to your function using setTimeout("qtycal", 100). It will not be noted by your users and will make your calculation works.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端