2014-11-06 18 views
-1

我正在創建HTML發票。我們使用的系統將數據輸出到HTML中的HTML。我把相關的標籤(如{{order_purchasedate}}),並在那裏顯示值。在Html中添加值

我需要發票來顯示訂單中所有項目的總訂單重量。權重將顯示爲列表中的數字,例如「60 80 120」,只有一個空格將它們分開。

如何添加數字是html,只顯示總數?

我知道我可以使用Java,但我不熟悉這一點。如果有人能夠幫助那會很好。

*編輯我已經嘗試了代碼。我正在努力獲得輸出。我相信你們必須認爲我是個白癡!

<!DOCTYPE html> 
<html> 
<body> 

<h1>Invoice</h1> 

<p id="order_weight"></p> 

<script> 
getTotal = function(values){ 

    var total = values.split(" ").reduce(function(a,b){ 

     a = parseInt(a) 
     b = parseInt(b) 

     return a+b 

    }); 

    return total 
} 
document.getElementById("order_weight").innerHTML = getTotal(a+b); 
</script> 

</body> 
</html> 
+0

你能提供一個代碼示例嗎?你想要達到的目的不能用HTML完成,所以一些代碼可能會有幫助,因爲在這種情況下,javascript或php可能會有所幫助。 – Kommodore 2014-11-06 10:30:12

回答

1

您可以使用JavaScript獲取您的值並計算總值。

getTotal = function(values){ 

    var total = values.split(" ").reduce(function(a,b){ 

     a = parseInt(a) 
     b = parseInt(b) 

     return a+b 

    }); 

    return total 
} 

,然後只需要調用該函數

getTotal(x) 

其中x是你的清單

這裏是工作示例: http://jsfiddle.net/o89f1mj5/1/

+0

感謝您的幫助。我仍然在學習這一切,這裏有這樣的知識庫。我將如何在我的HTML代碼中加入? – Ben 2014-11-06 13:04:02

+0

歡迎您。需要看到你的代碼能夠幫助你實現。 – mgibala 2014-11-06 14:01:05

0
<body> 

<h1>Invoice</h1> 

<p id="order_weight"></p> 
    <input id="total" name="total" type="text"></input> 
    <input id="totalBtn" type="button" value="Get Total" onClick="getTotal();"></input> 
</body> 

<script> 
    function getTotal() { 
    var values = $('#total').val(); 
    alert(values); 
    var total = values.split(" ").reduce(function(a,b){ 

     var a = parseInt(a); 
     var b = parseInt(b); 

     return a+b; 

    }); 

    document.getElementById('order_weight').innerHTML = "Value: " + total; 
} 

</script> 

工作JS小提琴:http://jsfiddle.net/o89f1mj5/9/

在的jsfiddle把「5 15 30」到文本框(當然沒有引號),它將使該段是50

這是@mgibala前面所做的那樣使用文本框和一個按鈕。他的回答應該被接受,我只是在給你一個可變輸入選項的應用程序。