好的,所以我必须将用户输入的成人人数乘以我获得的旅程费用的乘积,但是我还需要考虑孩子的人数,并在所有旅程中给他们统一的20%折扣率,这是我的代码。
的HTML
<select id = "journeyList">
<option name ="Bristol " value="40">Bristol - Newcastle</option>
<option name ="London " value="35" >Bristol - London</option>
<option name ="Glasgow " value="70" >Glasgow - Manchester</option>
</select><br><br>
Number of Adults:
<form>
<input type = "number" name ="adult" id="ofAdults" value="" required><br>
Number of Children:<br>
<input type = "number" name ="child" id="ofChild" value="" required>
<button type="button" onclick="BookingFare(); return false;">Submit</button><br>
Journey Price:
<div id="priceBox"></div><br>
Number of Adults:
<div id="adultsBox"></div><br>
Number of Children:
<div id="childBox"></div><br>
Total Cost:
<div id="totalBox"></div>
Java脚本
function BookingFare() {
var journeyList = document.getElementById("journeyList");
var price =journeyList.options[journeyList.selectedIndex].value;
var Adults = document.getElementById("ofAdults").value;
var Children = document.getElementById("ofChild").value;
//var total = price * Adults;
document.getElementById('priceBox').innerHTML = price;
document.getElementById('adultsBox').innerHTML = Adults;
document.getElementById('childBox').innerHTML = Children;
document.getElementById('totalBox').innerHTML = total;
}
我应该寻找if语句吗?谢谢您的任何建议!
最佳答案
减少20%等于乘以.8,因此:
document.getElementById('totalBox').innerHTML =
(Adults * price) + (Children * price * .8);
关于javascript - 用多个变量计算旅程费用的总价,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28881592/