duangu1033 2015-06-18 14:54
浏览 89
已采纳

HTML表单输入javascript autosum总价格支付

I have the follow code to sell a product, if I apply discount it works, same if I apply taxes, all prices per unit (quantity 1) is working, but the total price to pay if I change quantity is not working

What I need is to autofill to later save to mysql the follow fields:

default price per unit (comming from mysql) = working
apply taxes or not per unit = working
apply discount per unit = working
price per unit after discount and after taxes = working

total price to pay if I change quantity from 1 to 2 o 3 etc = not working this must be autofilled by multiplicate price to pay after taxes per unit by quantity = not working

here is the code

<table width="339" border="0" cellpadding="0">
  <tr>
    <td width="98">Pay taxes?</td>
    <td width="115">Make Discount?</td>
    <td width="118">Default unit price</td>
     <td>Quantity</td>
  </tr>
  <tr>
    <td>
      <select name="taxes" class="select">
        <option value="0" selected>no taxes</option>
        <option value="19">19% taxes</option>
      </select>
    </td>
    <td>
      <select name="discount" class="select">
        <option value="0" selected>no discount</option>
        <option value="5">5% discount</option>
        <option value="10">10% discount</option>
        <option value="20">20% discount</option>
      </select>
    </td>
    <td>
      <input type="text" name="cost" class="input140" value="1000">
    </td>
    <td><input type="text" name="quantity" value="1"></td>
  </tr>
  <tr>
    <td>Unit price after discount</td>
    <td>Tax per unit</td>
    <td>Total Price per unit</td>
     <td>Total Price to pay per quantity</td>
  </tr>
  <tr>
    <td><input type="text" name="price" value="1000"></td>
    <td><input type="text" name="ttaxes" value="0"></td>
    <td><input type="text" name="total" value="1000"></td>
    <td><input type="text" name="totaltopay" value="1000"></td>
  </tr>
 </table><script>
/**
 * Elements
 */
var taxes    = document.getElementsByName('taxes')[0];
var discount = document.getElementsByName('discount')[0];
var cost     = document.getElementsByName('cost')[0];
var price    = document.getElementsByName('price')[0];
var ttaxes   = document.getElementsByName('ttaxes')[0];
var total    = document.getElementsByName('total')[0];


/**
 * Calculations
 */
function updateInput() {
  price.value = cost.value - (cost.value * (discount.value / 100));
  ttaxes.value = (price.value * (taxes.value / 100));
  var sum = parseFloat(price.value) + parseFloat(ttaxes.value);
  total.value = sum.toFixed(0);
}


/**
 * Event Listeners
 */
taxes.addEventListener('change', updateInput);
discount.addEventListener('change', updateInput);
cost.addEventListener('change', updateInput);
cost.addEventListener('keyup', updateInput);
</script>

Here is the demo Fiddle

https://fiddle.jshell.net/63dkvgs5/

image example of what I need

enter image description here

  • 写回答

1条回答 默认 最新

  • dongtuo2373 2015-06-18 14:58
    关注

    You forgot to add quantity!

    var quantity = document.getElementsByName('quantity')[0];
    quantity.addEventListener('keyup', updateInput);
    

    And update the calculation:

    var sum = (parseFloat(price.value) + parseFloat(ttaxes.value)) * quantity.value;
    

    Fiddle: https://fiddle.jshell.net/praveenscience/v6spxoqv/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信