我试图开始工作的更大代码的一个小例子。
我很新,但我能够批评。
<script>
var costPeeler;
var costCutter;
var costMachine;
var machineType;
function totalIt() {
var costPeeler = 5850;
var costCutter = 4950;
var machineType = document.getElementById("machineType").value;
if (machineType=peelerType) {
var costMachine = costPeeler;
} else if (fryType=cutterType) {
var costMachine = costCutter;
} else {
var costMachine = costPeeler + costCutter;
}
}
</script>
<form>
<input type="radio" name="machineType" value="peelerType" checked onkeyup="totalIt();">Potato Peeler
<input type="radio" name="machineType" value="cutterType" onkeyup="totalIt();">Potato Fry Cutter
<input type="radio" name="machineType" value="bothType" onkeyup="totalIt();">Both
</form>
<input name="costMachine" id="costMachine">
如果有人可以提供提示或建议,将不胜感激!
谢谢!
真的很感激建议!这正是我做错了。
问题,我将新信息与我的完整代码合并,但它无效。 我已经经历了这么多次,我找不到问题
<html>
<head>
<script>
function totalIt() {
var costPeeler = 6350;
var costCutter = 5495;
var hourlyPay = 10.25;
var benefit = 1.1;
var hoursWorked = 0;
var costTotal = 0;
var costPound = 0;
var consumption = 0;
var costMachine = 0;
var cost50 = 0;
var hoursWorked = 0;
var fridgeCost = 0;
var cost1year = 0;
var saving1year = 0;
var saving2year = 0;
var savingMonth = 0;
var returnOnI = 0;
var saving20year = 0;
var laborbenefitsCost = 0;
var materialslaborCost = 0;
var potatoUse = document.form1["potatoUse"].value;
var fryType = document.form1["fryType"].value;
var machineType = document.form1["machineType"].value;
if (fryType=="frozenType") {
var cost50 = 50;
var hoursWorked = 0;
var fridgeCost = 1;
} else if (fryType=="unwashedType") {
var cost50 = 10;
var hoursWorked = 0.75;
var fridgeCost = 0;
} else {
var cost50 = 17;
var hoursWorked = 0.25;
var fridgeCost = 0;
}
if (machineType=="peelerType") {
costMachine = costPeeler;
} else if (machineType=="cutterType") {
costMachine = costCutter;
} else {
costMachine = costPeeler + costCutter;
}
var laborCost = hourlyPay * hoursWorked + 0;
var laborbenefitsCost = laborCost * 0.1 + laborCost + 0;
var materialslaborCost = cost50 + laborbenefitsCost;
var costsTotal = materialslaborCost + fridgeCost;
var costPound = costsTotal / 50;
var consumption = potatoUse * 52;
var cost1Year = costPound * consumption;
var costSaving = costMachine + 0.21 * consumption;
var saving1year = cost1year - costSaving;
var saving2year = cost1year - costsaving - costMachine;
var savingMonth = saving2year / 12;
var returnOnI = costMachine / savingMonth;
var saving20year = saving2year * 19 + saving1year;
document.getElementById("costMachine").value = costMachine;
document.getElementById("cost50").value = cost50;
document.getElementById("hoursWorked").value = hoursWorked;
document.getElementById("fridgeCost").value = fridgeCost;
document.getElementById("hourlyPay").value = hourlyPay;
document.getElementById("hoursWorked").value = hoursWorked;
document.getElementById("costTotal").value = costTotal;
document.getElementById("costPound").value = costPound;
document.getElementById("consumption").value = consumption;
document.getElementById("cost1year").value = cost1year;
document.getElementById("saving1year").value = saving1year;
document.getElementById("saving2year").value = saving2year;
document.getElementById("savingMonth").value = savingMonth;
document.getElementById("returnOnI").value = returnOnI;
document.getElementById("saving20year").value = saving20year;
}
</script>
</head>
<body>
<form name="form1">
<table width="100%" border="0">
<tbody>
<tr>
<td colspan="5" align="center">Weekly Potato Use:
<input name="potatoUse" id="potatoUse" onKeyUp="totalIt();" /></td>
</tr>
<tr>
<td colspan="5" align="center">
<input type="radio" name="machineType" value="peelerType" checked onClick="totalIt();" />Potato Peeler
<input type="radio" name="machineType" value="cutterType" onClick="totalIt();" />Potato Fry Cutter
<input type="radio" name="machineType" value="bothType" onClick="totalIt();" />Both</td>
</tr>
<tr>
<td colspan="5" align="center">
<input type="radio" name="fryType" value="frozenType" checked onClick="totalIt();" />Frozen French Fries
<input type="radio" name="fryType" value="unwashedType" onClick="totalIt();" />Raw Unwashed Potatoes
<input type="radio" name="fryType" value="washedType" onClick="totalIt();" />Raw Washed Potatoes</td>
</tr>
<tr>
<td colspan="5"></td>
</tr>
<tr>
<td>Cost of 50 lbs</td>
<td>Total Costs</td>
<td>Cost per Pound</td>
<td>Machine Cost</td>
<td>Total Operating Costs 1 year</td>
</tr>
<tr>
<td><input name="cost50" id="cost50"></td>
<td><input name="costTotal" id="costTotal"></td>
<td><input name="costPound" id="costPound"></td>
<td><input name="costMachine" id="costMachine"></td>
<td><input name="cost1year" id="cost1year"></td>
</tr>
<tr>
<td>1st Year Savings</td>
<td>2nd Year Savings</td>
<td>Savings per Month</td>
<td>Return on Investment</td>
<td>Total Savings (20 Years)</td>
</tr>
<tr>
<td><input name="saving1year" id="saving1year"></td>
<td><input name="saving2year" id="saving2year"></td>
<td><input name="savingMonth" id="savingMonth"></td>
<td><input name="returnOnI" id="returnOnI"></td>
<td><input name="saving20year" id="saving20year"></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
答案 0 :(得分:0)
您需要了解两件事: 1)分配操作员 2)条件运算符
对于条件,使用条件运算符而不是赋值运算符。因为如果您使用上面使用过的赋值运算符,它将始终为TRUE
if (machineType=peelerType) {
这应该是
if (machineType==peelerType) {
....
else if (fryType==cutterType) {
答案 1 :(得分:0)
您的代码存在多个问题:
以下是working example以下代码:
<html>
<head>
<script>
function totalIt() {
var costMachine = 0;
var costPeeler = 5850;
var costCutter = 4950;
var machineType = document.form1["machineType"].value;
if (machineType=="peelerType") {
costMachine = costPeeler;
} else if (machineType=="cutterType") {
costMachine = costCutter;
} else {
costMachine = costPeeler + costCutter;
}
document.getElementById("costMachine").value = costMachine;
}
</script>
</head>
<body>
<form name="form1">
<input type="radio" name="machineType" value="peelerType" checked onclick="totalIt();" />Potato Peeler
<input type="radio" name="machineType" value="cutterType" onclick="totalIt();" />Potato Fry Cutter
<input type="radio" name="machineType" value="bothType" onclick="totalIt();" />Both
<input name="costMachine" id="costMachine" value="5850" />
</form>
</body>
</html>
我还建议用switch语句替换你的if-else-if模式,添加标签标签,动态加载削皮器价格而不是硬编码,但这应该足以让你开始。