duanliao5995 2014-11-22 18:50
浏览 25

编辑数量使用Javascript价格将调整

I'm currently doing an project on online shopping cart, whereby customers can edit the quantity before checkout. I wanted to make it such that when customer edit the quantity, the price of the item and total price of all items will be adjusted. I know I can achieve that though javascript. Can't really work and understand it. It will be nice if someone would help me. Thank you so so much! Ps: I am not sure if my codes are correct, but here's what I tried:

<script type="text/javascript">
    function update(form){
    var quantity = document.getElementById('#quantity');
    var price = quantity * ($list['price']);
    document.getElementById("price").value = price;
    ​}
    </script>`

I have not done for the total price yet because the price itself isn't working. I know there's something I miss out. Hope someone can enlighten me.

My HTML:

`//database query

<td align='middle'><input type="number"  onChange="update(this)"  id="quantity" name="quantity"   min="1" max='<?php echo $list['quantity'] ?>' value='1'></td>

<td><input type="text" id="price" value='<?php echo $list['price'] ?>' readonly></td>

<td><input type="text" name="amount" id="amount"  readonly></td>`

I may have made stupid mistakes,so it will be nice if someone would explain about how javascript works if I got the wrong concepts. Thanks so much for helping!!:)

  • 写回答

2条回答 默认 最新

  • dongruolin5324 2014-11-22 19:17
    关注

    see demo here

    look this line

    quantity = document.getElementById('#quantity');
    

    in this line remove '#'

    quantity = document.getElementById(**'quantity'**);
    

    and use parseint to convert to integer

    var price = quantity * ($list['price']);
    

    $list['price'] is false

    to get price you need to do like this:

    document.getElementById('price').value;
    

    final code is :

    form.php

    //database query
    
    <td align='middle'><input type="number"  onChange="update(this)"  id="quantity" name="quantity"   min="1" max='<?php echo $list['quantity'] ?>' value='1'></td>
    
    <td><input type="text" id="price" value='<?php echo $list['price'] ?>' readonly></td>
    
    <td><input type="text" name="amount" id="amount"  readonly></td>`
    

    script.js

        <script type="text/javascript">
            function update(form){
        var quantity = document.getElementById('quantity').value;
    
        var price = document.getElementById("price").value;
        price = parseInt(price);
        quantity = parseInt(quantity);
    
        var amount= quantity * price ;
    
        document.getElementById("amount").value = amount;
    }
    
    
            </script>`
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题