Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables If Else无线电框分配变量_Variables_If Statement_Radio Button - Fatal编程技术网

Variables If Else无线电框分配变量

Variables If Else无线电框分配变量,variables,if-statement,radio-button,Variables,If Statement,Radio Button,我正在努力工作的一段较大代码的小示例 我是个新手,但我能接受批评 <script> var costPeeler; var costCutter; var costMachine; var machineType; function totalIt() { var costPeeler = 5850; var costCutter = 4950; var machin

我正在努力工作的一段较大代码的小示例

我是个新手,但我能接受批评

<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>

有两件事你需要了解: 1个赋值运算符 2条件运算符

对于条件,使用条件运算符而不是赋值运算符。因为如果您像上面一样使用赋值运算符,它将始终为真

if (machineType=peelerType) {
这应该是

    if (machineType==peelerType) {
....
    else if (fryType==cutterType) {

您的代码存在多个问题:

document.getElementByIdmachineType引用了页面上不存在的ID var costMachine在多个作用域中定义,当前实现时不可用 赋值运算符=代替相等运算符使用== 比较需要对照字符串值进行检查 使用keyup事件代替onclick事件 表单外的输入计算机 下面是以下代码的一个示例:

<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模式,添加标签标签,动态加载削皮机价格,而不是硬编码,但这应该足以让您开始使用。

这太棒了!谢谢你的帮助。