Javascript 到处都是计算。需要数学天才
函数计算不正确 上下文: 头等舱票价是经济舱票价的2倍。 商务舱票价是经济舱票价的1.5倍。 往返票价是单程票价的2倍 单程经济舱票价的路线成本(基本成本) 霍金斯-里弗代尔229美元 霍金斯-哈登菲尔德259美元 霍金斯-罗克韦尔199美元 霍金斯-伊格尔顿179美元 霍金斯-波尼179美元 霍金斯-双峰239美元 功能应该是添加所选选项并显示总数。 怎么了Javascript 到处都是计算。需要数学天才,javascript,Javascript,函数计算不正确 上下文: 头等舱票价是经济舱票价的2倍。 商务舱票价是经济舱票价的1.5倍。 往返票价是单程票价的2倍 单程经济舱票价的路线成本(基本成本) 霍金斯-里弗代尔229美元 霍金斯-哈登菲尔德259美元 霍金斯-罗克韦尔199美元 霍金斯-伊格尔顿179美元 霍金斯-波尼179美元 霍金斯-双峰239美元 功能应该是添加所选选项并显示总数。 怎么了 <!DOCTYPE html> <html> <head> </head&g
<!DOCTYPE html>
<html>
<head>
</head>
<script>
function calculation() {
var cost = 0; //Base cost of flight (one way Economy)
var radioButton; // A radio button
var selection = 0; // The selected radio button, 1 to 6 going down.
for (var i = 1; i <= 6; i++) {// Get the number of the selection (1 to 6)
radioButton = document.getElementById("destination" + i);
if (radioButton.checked == true) {
selection = i;
}
}
if (selection == 1) {
cost = 229
} else if (selection == 2) {
cost = 259
} else if (selection == 3) {
cost = 199
} else if (selection == 4) {
cost = 179
} else if (selection == 5) {
cost = 179
} else if (selection == 6) {
cost = 239
} else if (selection == 0) {
alert("Please select a flight.");
return false;
}
var seating = 0;
for (var x = 1; x <= 3; x++) {
radioButton = document.getElementById("seating" + x);
if (radioButton.checked == true) {
seating = x;
}
}
var totalcost = 0;
if(seating == 0){
alert("Please select a seat.");
return false;
} else if(seating == 1){
totalcost = cost + (cost * 2);
} else if(seating == 2){
totalcost = cost + (cost * 1.5);
} else if(seating == 3){
totalcost = cost;
}
if(document.getElementById("return").checked){
totalcost = totalcost*2;
}
totalcost = totalcost + cost;
alert("Total cost: "+totalcost);
}
</script>
<body>
<h1> Hawkins Airlines Fare Calculator</h1>
<p> Complete the form below to calculate the cost of your flight.</p>
<form onsubmit="return false;">
<p>Route:</p>
<input type="radio" name="destination" id="destination1"> Hawkins - Riverdale<br>
<input type="radio" name="destination" id="destination2"> Hawkins - Haddonfield<br>
<input type="radio" name="destination" id="destination3"> Hawkins - Rockwell<br>
<input type="radio" name="destination" id="destination4"> Hawkins - Eagleton<br>
<input type="radio" name="destination" id="destination5"> Hawkins - Pawnee<br>
<input type="radio" name="destination" id="destination6"> Hawkins - Twin Peaks<br>
<br>
<input type="checkbox" name="appliances" id="return">
<label>Click here if you will be purchasing a return fare </label><br>
<p>Seating class:</p>
<input type="radio" name="seating" id="seating1"> First seating<br>
<input type="radio" name="seating" id="seating2"> Business seating<br>
<input type="radio" name="seating" id="seating3"> Economy seating<br>
<br>
<button onclick="calculation()"> Calculate </button>
<input type="reset" value="Restore Defults">
</form>
</body>
</html>
函数计算(){
var cost=0;//基本飞行成本(单程经济型)
var radioButton;//单选按钮
var selection=0;//所选单选按钮,1到6向下。
对于(var i=1;i,如果您有适当的后端验证:
<form onsubmit="return false;">
<p>Route:</p>
<input type="radio" name="destination" value="229" id="destination1"> Hawkins - Riverdale<br>
<input type="radio" name="destination" value="259" id="destination2"> Hawkins - Haddonfield<br>
<input type="radio" name="destination" value="199" id="destination3"> Hawkins - Rockwell<br>
<input type="radio" name="destination" value="179" id="destination4"> Hawkins - Eagleton<br>
<input type="radio" name="destination" value="179" id="destination5"> Hawkins - Pawnee<br>
<input type="radio" name="destination" value="239" id="destination6"> Hawkins - Twin Peaks<br>
<br>
<input type="checkbox" name="appliances" id="return">
<label>Click here if you will be purchasing a return fare </label><br>
<p>Seating class:</p>
<input type="radio" name="seating" value="2" id="seating1"> First seating<br>
<input type="radio" name="seating" value="1.5" id="seating2"> Business seating<br>
<input type="radio" name="seating" value="1" id="seating3"> Economy seating<br>
<br>
<button onclick="calculation()"> Calculate </button>
<input type="reset" value="Restore Defults">
</form>
什么是radioButton,什么是Selection,因为问题详细信息中的内容不在函数本身中,请修复变量命名和问题标题,但由于某些原因,您在结尾处的总权利增加了一倍(totalcost=totalcost+cost;
),因此可能会删除该选项。
function calculation() {
//select selected radio buttons
const selectedRoute = document.querySelector('input[name="destination"]:checked')
const selectedSeatingClass = document.querySelector('input[name="seating"]:checked')
const isRoundTrip = document.querySelector('input[name="appliances"]').checked
//show "Select route" alert if no route is selected
if (selectedRoute === null) {
alert('Select route')
//do not continue calculation
return false;
}
//Do the same, just for seating
if (selectedSeatingClass === null) {
alert('Select seating class')
//do not continue calculation
return false;
}
//grab values off selected radio buttons
//wrap them in Number to get a numeric value because
//you always get string when reading a value off an input
const cost = Number(selectedRoute.value)
const seatingMultiplier = Number(selectedSeatingClass.value)
const price = cost * seatingMultiplier
isRoundTrip ? alert(price * 2) : alert(price)
}