更新文本字段的keyup值

时间:2015-06-19 06:20:19

标签: javascript jquery html ajax onkeyup

我有以下代码:

{% for product in app.session.get('aBasket') %}
    <input id="product_quantity_{{ product['product_id'] }}" class="form-control quantity" type="text" value="{{ product['product_quantity'] }}" name="" onkeyup="calculatePrice({{ product['product_price'] }},{{ product['product_id'] }})">
    <span id="total_price_{{ product['product_id'] }}">{{ product['product_quantity'] * product['product_price'] }}</span>
{%endfor}
<div id="total_price_basket">TOTAL:0</div>

我的js功能:

<script type="application/javascript">
    function calculatePrice(price, product_id) {
        var x = document.getElementById("product_quantity_"+product_id);
        var total_price = price * x.value;
        $("#total_price_"+product_id).html(total_price);
        $("#total_price_basket").html();
    }
</script>

那么,它所有产品的价格合理,问题是如何更改div的值&gt; total_price_basket onkeyup?我必须收集每种产品的数量。你能帮我吗? Thx提前

5 个答案:

答案 0 :(得分:1)

使用parseFloatstring转换为float

  

parseFloat()函数解析字符串参数并返回浮点数。

见内联评论:

var total_price = 0; // Set default to zero
// ^^^^^^^^^^^^^^^^^
function calculatePrice(price, product_id) {
    var x = document.getElementById("product_quantity_" + product_id);
    var total_price = parseFloat(price) * parseFloat(x.value);
    //                ^^^^^^^^^^          ^^^^^^^^^^
    $("#total_price_" + product_id).html(total_price);
    $("#total_price_basket").html();
}

答案 1 :(得分:1)

我会使用jQuery并摆脱突兀的内联事件处理程序。对于此HTML标记,将稍微更改以使用数据属性:

{% for product in app.session.get('aBasket') %}
    <input data-id="{{ product['product_id'] }}" 
           data-price="{{ product['product_price'] }}"
           value="{{ product['product_quantity'] }}"
           class="form-control quantity" type="text">
    <span class="total" id="total_price_{{ product['product_id'] }}">{{ product['product_quantity'] * product['product_price'] }}</span>
{%endfor}
<div id="total_price_basket">TOTAL:0</div>

JS:

$('.quantity').on('keyup', function () {

    // Update individual price
    var price = $(this).data('price') * this.value;
    $('#total_price_' + $(this).data('id')).text(price);

    // Update total
    var total = 0;
    $('.total').each(function() {
        total += Number($(this).text());
    });
    $('#total_price_basket').text('TOTAL: ' + total);
})
// Trigger initial calculation if needed
.trigger('keyup');

演示: http://jsfiddle.net/c64xfrpr/

答案 2 :(得分:0)

最初,在渲染页面时,将total_price_basket设置为服务器中的值。  在您的javascript中设置一个全局变量来存储所有产品的总和:

var sum = 0;

然后在你的calculatePrice方法中添加以下两行:

sum += total_price;
$("#total_price_basket").html('TOTAL: ' + sum);

还要在删除产品时减去。

<script type="application/javascript">
    var sum = 0;

    function calculatePrice(price, product_id) {
        var x = document.getElementById("product_quantity_"+product_id);
        var total_price = price * x.value;
        $("#total_price_"+product_id).html(total_price);
        sum += total_price;
        $("#total_price_basket").html('TOTAL: ' + sum);
    }
    </script>

答案 3 :(得分:0)

你可以试试这个:

$(function(){
var price=100;
$('input.quantity').keyup(function(){

var all=$('input.quantity');
var total=0;
for(var i=0;i<all.length;i++){
   if(!isNaN(all[i].value)){
        total+=price*all[i].value
    }   
}
$("#total_price_"+product_id).html('TOTAL : '+total);
});   

});

答案 4 :(得分:0)

试试这个

var x = document.getElementById("product_quantity_"+product_id);
        var total_price = price * x.value;
        console.log(x.value);