我正在尝试实现一个重量转换器,它需要磅(磅)并将其转换为kg,反之亦然,我似乎无法理解它的正确性,这是我的代码,我不知道我不想使用任何库或框架,因为我想学习JavaScript的基础知识,也不知道如何实现折扣选项。
这是我能够做到的。
var appleWeight = +document.getElementById('weight1').value;
var orangeWeight = +document.getElementById('weight2').value;
var grapeWeight = +document.getElementById('weight3').value;
var bananaWeight = +document.getElementById('weight4').value;
var totalWeight = appleWeight + orangeWeight + grapeWeight + bananaWeight;
document.getElementById('total_weight').innerHTML = totalWeight;
console.log(totalWeight)
var applePrice = +document.getElementById('price1').value;
var orangePrice = +document.getElementById('price2').value;
var grapePrice = +document.getElementById('price3').value;
var bananaPrice = +document.getElementById('price4').value;
var totalPrice = applePrice + orangePrice + grapePrice + bananaPrice;
var discount = document.getElementById('discount').value;
var discountAmount = (totalPrice * Number(discount)) / 100
var finalPrice = totalPrice - discountAmount
document.getElementById('total_price').innerHTML = finalPrice;
console.log("total price " + totalPrice)
var lb = document.getElementsByTagName('select'.value)
<h1>JS Grocery Shop</h1>
<span class="label">Apple</span> <input type="number" name="weight" id="weight1" value="3.5" />
<select><option>kg</option><option class="lb" value="lb">lb</option></select> @ $<input type="number" name="price" id="price1" value="2.3" /><br />
<span class="label">Orange</span><input type="number" name="weight" id="weight2" value="5" />
<select><option>kg</option><option class="lb" value="lb">lb</option></select> @ $<input type="number" name="price" id="price2" value="3.0" /><br />
<span class="label">Grape</span> <input type="number" name="weight" id="weight3" value="2.0" />
<select><option>kg</option><option class="lb" value="lb">lb</option></select> @ $<input type="number" name="price" id="price3" value="1.5" /><br />
<span class="label">Banana</span><input type="number" name="weight" id="weight4" value="1" />
<select><option>kg</option><option class="lb" value="lb">lb</option></select> @ $<input type="number" name="price" id="price4" value="0.5" /><br /> Discount : <select id="discount">
<option value="0">0</option>
<option value="5">5%</option>
<option value="10">10%</option>
<option value="15">15%</option>
<option value="20">20%</option>
</select>
<br><br> Total Price : <span id="total_price"></span><br /> Total Weight : <span id="total_weight"></span>