簡體   English   中英

更新文本字段的keyup值

[英]Update on keyup value of a text field

我有以下代碼:

{% 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> total_price_basket onkeyup的值? 我必須收集每種產品的數量。 你能幫我嗎? 提前Thx

使用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();
}

我將使用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/

最初,在渲染頁面時,將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>

您可以嘗試以下方法:

$(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);
});   

});

試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM