duanqun7761 2014-05-22 13:16
浏览 41
已采纳

输入更改更新价格div

So basically what I am trying to do is a simple cart update, with the total price and quantity change -> price update. I have ran into a problem which I cannot solve, jQuery is not my language but I understand the basic of it.

And I would appreciate if you could tell me what I did wrong in that code and what I should think about in the future also if the total price will be able to update using two diffrent items and quantity.

<table cellspacing="2" cellpadding="0" border="0" width="280">
    <tr>
        <td width="140" valign="top" align="left"><div class="product_name">T-shirt, Black XS</div><div class="product_serial">Art.nr: <a href="javascript:void(0);">#TS4502-BL</a></td>
        <td width="30" valign="top" align="center"><input type="text" id="quantity" value="1" maxlength="2" /></td>
        <td width="80" valign="top" align="center"><div class="product_total_cost" id="price" value="199">199 kr</div></td>
        <td width="9" valign="top" align="right"><div class="remove_product">X</div></td>
    </tr>
</table>

<table cellspacing="2" cellpadding="0" border="0" width="280">
    <tr>
        <td width="110" valign="middle"><div class="procental_off">-10%</div><div class="cart_total_cost" id="total"> 358,2 kr</div></td>
        <td width="70" valign="middle" align="center"><div class="cart_clear">Rensa</div></td>
        <td width="100" valign="top" align="right"><div class="cart_continue">Fortsätt</div></td>
    </tr>
</table>

With the following jQuery code I found on the internet and tried to make it work with my code.

<script type="text/javascript">
    $('#quantity').change(function(event) {
        $('#price').html($('#quantity').val()* $('#price').val());
    }); 
</script>
  • 写回答

5条回答 默认 最新

  • dtmsaqtly798322992 2014-05-22 13:23
    关注

    #price is a div and does not have a value (even if you typed it, it's invalid)

    You may want to consider something like this

    $('#quantity').change(function (event) {
        $('#price').html($(this).val() * parseInt($('#price').text(), 10));
          // parseInt converts in this case strings to integers
    });
    

    EDIT: The problem now is that you have to reset the price... didn't see that at first

    http://jsfiddle.net/Spokey/8hfxU/1/


    NEW: You can for example store the price in a data-* attribute and retrieve it from there

    http://jsfiddle.net/Spokey/8hfxU/2/

    <div class="product_total_cost" id="price" data-val="199">199 kr</div>
    

    And script

    $('#quantity').change(function (event) {
        $('#price').html($(this).val() * parseInt($('#price').attr('data-val'), 10));
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体