如何制作JavaScript基本购物车金额计算器

时间:2017-12-24 21:09:55

标签: javascript cart shopping-cart



var clone = GetClone(5);
clone.Role = PartyRole.Whatever;




取第一个输入类型="数字" value1 * 100显示在输出框中,然后输入type =" number" value2 * 100显示在第二个输出框中,然后添加value1和value2并显示在输出框3中。我使用的是JavaScript并且是初学者。

您在阅读问题时能看到设计吗?

它正在为output1和output2工作,但是无法为第三个输出框中的output1和output2的总输出创建正确的方法或函数。

2 个答案:

答案 0 :(得分:1)

grandtotalfruit()apple()函数中添加banana()

 function apple() {
        var itemquantity = document.getElementById("apple");
        var appleamount = document.getElementById("appletotal");
        var appletotalamount = itemquantity.value * 50;
        appleamount.value = appletotalamount;
        grandtotalfruit();
      }

      function banana() {
        var itemquantity = document.getElementById("banana");
        var bananaamount = document.getElementById("bananatotal");
        var bananatotalamount = itemquantity.value * 30;
        bananaamount.value = bananatotalamount;
        grandtotalfruit();
      }

答案 1 :(得分:1)

基本上,@ dev8989说的是什么。每次用户更改" apples"价值,你改变"苹果数量" (同样适用于"香蕉")。您还必须更新"总计"如果有任何苹果/香蕉更新。这就是@ dev8989建议的内容:



var $apples = document.getElementById('apples')
var $applesTotal = document.getElementById('apples-total')
var $bananas = document.getElementById('bananas')
var $bananasTotal = document.getElementById('bananas-total')
var $grandTotal = document.getElementById('grand-total')

function apple (v) { return 50 * v }
function banana (v) { return 30 * v}

$apples.addEventListener('change', function() {
  $applesTotal.textContent = apple($apples.value)
  $grandTotal.textContent = apple($apples.value) + banana($bananas.value)
})
$bananas.addEventListener('change', function() {
  $bananasTotal.textContent = banana($bananas.value)
  $grandTotal.textContent = apple($apples.value) + banana($bananas.value)
})

<input type="number" id="apples">
<p>Apples total: <span id="apples-total">0</span></p>
<input type="number" id="bananas">
<p>Bananas total: <span id="bananas-total">0</span></p>
<p>Grand total: <span id="grand-total">0</span></p>
&#13;
&#13;
&#13;

编辑:这里修复了错误的原始代码。请参阅我对此必要性的评论:

&#13;
&#13;
var itemquantity, appleamount, appletotalamount = 0, bananaamount, bananatotalamount = 0, gtotalfruits, gtotalfruitss

function apple() {
  var itemquantity = document.getElementById("apple");
  var appleamount = document.getElementById("appletotal");
  appletotalamount = itemquantity.value * 50;
  appleamount.value = appletotalamount;
  grandtotalfruits();
}

function banana() {
  var itemquantity = document.getElementById("banana");
  var bananaamount = document.getElementById("bananatotal");
  bananatotalamount = itemquantity.value * 30;
  bananaamount.value = bananatotalamount;
  grandtotalfruits();
}

function grandtotalfruits() {
  var gtotalfruits = document.getElementById("grandtotalfruitss");
  var gtotalfruitss = appletotalamount + bananatotalamount;
  gtotalfruits.value = gtotalfruitss;
}
&#13;
<div class="left">
  <p class="fruit11">Apples - <input type="number" id="apple" class="shoppinginput" onChange="apple()" onchange="grandtotalfruits"> Rs.50/Kg</p>
  <p class="fruit12">Total amount = Rs.<output id="appletotal"></output></p>
</div>
<div class="left">
  <p class="fruit11">Bananas - <input type="number" id="banana" class="shoppinginput" onChange="banana()" onchange="grandtotalfruits"> Rs.30/Kg</p>
  <p class="fruit12">Total amount = Rs.<output id="bananatotal"></output></p>
</div>

<div class="grandtotal">
  <p class="fruit13">Grand total = Rs.<output id="grandtotalfruitss"></output></p>
&#13;
&#13;
&#13;