gpt4 book ai didi

JavaScript:if else 语句多个条件

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:20 24 4
gpt4 key购买 nike

JavaScript 新手,正在尝试创建一个学费计算器。我需要知道居住地,他们是否会参加校园类(class)和学分,然后计算学费。我无法使 if else 语句起作用。该代码仅针对校园类(class)显示,但一旦成功,我将添加在线类(class)等。感谢您的帮助!

如果我在每个类类型之前询问居住问题,我就会使用这个计算器,但我只想问一次居住问题,然后将学费分配给 var onctut2。我还希望学生能够为 onchours1 输入小数,但我只能让整数起作用。我尝试了各种 if else 语句。

    <!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Tuition Calculator</title>
<script type="text/javascript">// <![CDATA[
function formHandler(form) {
var residency = eval(form.residency.value);
var onctut1 = eval(form.onctut1.value);
var onchours1 = eval(form.onchours1.value);
var onctut2 = 0;
var t1 = onctut2 * onchours1;
var totalhours = onchours1;
var oncfee1 = 0;
var techfee1 = 0;
var strecfee = 0;
//Calculate on campus tut
if (residency == "yes") && (onctut1 == "yes") {
onctut2 = 252.00;
} else if (residency == "no") && (onctut1 == "yes") {
onctut2 = 554.00;
} else {
onctut2 = 0;
}
//Calculate on campus fees
if (onchours1 == "1") {
oncfee1 = 75.25;
} else if (onchours1 == "2") {
oncfee1 = 87.5;
} else if (onchours1 == "3") {
oncfee1 = 99.75;
} else if (onchours1 == "4") {
oncfee1 = 169.5;
} else if (onchours1 == "5") {
oncfee1 = 181.75;
} else if (onchours1 == "6") {
oncfee1 = 194.0;
} else if (onchours1 == "7") {
oncfee1 = 433.25;
} else if (onchours1 == "8") {
oncfee1 = 445.5;
} else if (onchours1 == "9") {
oncfee1 = 457.75;
} else if (onchours1 == "10") {
oncfee1 = 470.0;
} else if (onchours1 == "11") {
oncfee1 = 482.25;
} else if (onchours1 == "12") {
oncfee1 = 494.5;
} else if (onchours1 == "13") {
oncfee1 = 506.75;
} else if (onchours1 == "14") {
oncfee1 = 519.0;
} else if (onchours1 == "15") {
oncfee1 = 531.25;
} else if (onchours1 == "16") {
oncfee1 = 543.5;
} else {
oncfee1 = 0;
}
//Calculate on tech fee
if (totalhours == "1") {
techfee1 = 11.;
} else if (totalhours == "2") {
techfee1 = 22.;
} else if (totalhours == "3") {
techfee1 = 33.;
} else if (totalhours == "4") {
techfee1 = 44.;
} else if (totalhours == "5") {
techfee1 = 55.;
} else if (totalhours == "6") {
techfee1 = 66.;
} else if (totalhours == "7") {
techfee1 = 77.;
} else if (totalhours == "8") {
techfee1 = 88.;
} else if (totalhours == "9") {
techfee1 = 99.;
} else if (totalhours == "10") {
techfee1 = 110.;
} else if (totalhours == "11") {
techfee1 = 121.;
} else if (totalhours == "12") {
techfee1 = 132.;
} else if (totalhours == "13") {
techfee1 = 143.;
} else if (totalhours == "14") {
techfee1 = 154.;
} else if (totalhours == "15") {
techfee1 = 165.;
} else if (totalhours == "16") {
techfee1 = 165.;
} else {
techfee1 = 0;
}
//Calculate on student rec fee
if (totalhours > "0") {
strecfee = 4;
}
var fees1 = oncfee1;
var fees = fees1 + techfee1 + strecfee;
var cost = t1 + fees;
form.t1.value = "$" + t1.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
form.fees.value = "$" + fees.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
form.costresult.value = "$" + cost.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
form.totalhours.value = "" + totalhours;
}
// ]]></script>
</head>
<body>
<form method="post" name="testform">
<div>
<table
id="AutoNumber1"
summary="calculate the cost per semester for
graduate students"
>
<tbody>
<tr>
<td colspan="2"><label for="residency">Are you a Resident? </label>
<select id="residency" name="residency">
<option selected="selected" value="0"> </option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select></td>
</tr>
<tr>
<td colspan="2"><label for="onctut1">Will you take an On campus class? </label>
<select id="onctut1" name="onctut1">
<option selected="selected" value="0"> </option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select></td>
</tr>
<tr>
<td colspan="2"><label for="onchours1">Select On campus class Credit Hours </label>
<select id="onchours1" name="onchours1">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
</select></td>
</tr>
<tr>
<td colspan="2"><br></td>
</tr>
<tr>
<td colspan="2"><button
name="CostResult"
onclick="javascript:formHandler(this.form)"
type="button"
> Calculate Cost </button></td>
</tr>
<tr>
<td colspan="2"><br></td>
</tr>
<tr>
<td><label for="t1">Tuition On Campus Class(es) </label></td>
<td><input id="t1" name="t1" size="10" type="text" /></td>
</tr>
<tr>
<td><label for="fees">Fees </label></td>
<td><input id="fees" name="fees" size="10" type="text" /></td>
</tr>
<tr>
<td><label for="costresult">Total Estimated Tuition/Fees Semester Cost </label></td>
<td><input
id="costresult"
name="costresult"
size="10"
type="text"
/></td>
</tr>
<tr>
<td><label for="totalhours">Total Credit Hours </label></td>
<td><input id="totalhours" name="totalhours" size="10" type="text" /></td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>

当我点击“计算成本”时,表格仍然是空白的。

最佳答案

if (residency == "yes") && (onctut1 == "yes") {
onctut2 = 252.00;
} else if (residency == "no") && (onctut1 == "yes") {
onctut2 = 554.00;
} else {
onctut2 = 0;
}

我的猜测是问题在于您需要在整个条件而不是每个单独的条件周围加上括号。

if (residency == "yes" && onctut1 == "yes") {
onctut2 = 252.00;
} else if (residency == "no" && onctut1 == "yes") {
onctut2 = 554.00;
} else {
onctut2 = 0;
}

看起来您还可以简化您的代码,而不是对 techfee1 变量使用大量的 if 语句,您可以这样做:

totalhours = +totalhours; // convert it to a number
techfee1 = totalhours * 11;
if (totalhours === 16) {
techfee1 = 165;
}
if (totalhours > 16 || totalhours < 1) {
techfee1 = 0;
}

关于JavaScript:if else 语句多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57190047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com