-4

我的 html 代码采用两个用户选择的输入数字并进行计算。在用户选择输入后,它无法实际进行计算。当我输入这两个数字并推动计算时,它什么也没做。

function calc(form)
{
if(isNaN(form.resistance.value))
 {
 alert("Error in input");
 return false;
 }

if(form.resistance.value.length > 32)
 {
 alert("Error in input");
 return false;
  }
 function calc(form1)
{
  if(isNaN(form1.strain.value))
 {
 alert("Error in input");
 return false;
}

 if(form1.strain.value.length > 32)
{
 alert("Error in input");
   return false;
} 



var Rchange = 2 * form1.strain.value * form.resistance.value;


 var newResistance =(parseInt(form.resistance.value) + Rchange);
document.getElementById("newResistance").innerHTML = chopTo4(newResistance);

}

function chopTo4(raw)
{
 strRaw = raw.toString();
 if(strRaw.length - strRaw.indexOf("0") > 4)
strRaw = strRaw.substring(0,strRaw.indexOf("0") + 5);
  return strRaw;
 }

</script>

</head>
<body>


<table>
<tr><form1><td>Enter Strain: </td><td><input style="text-align:right" type="text" name="strain"></td></tr>
</table>
<table>
<tr><form><td>Enter Resistance: </td><td><input style="text-align:right" type="text" name="resistance"></td></tr>
<tr><td colspan=2 align="center"><input type="button" value="Calculate" onClick="calc(this.form)"></td></form></tr>
<tr><td>New Resistance: </td><td id="newResistance"></td></tr>
</table>  
4

1 回答 1

0
function calc(form)
{
 if(isNaN(form.resistance.value))
{
 alert("Error in input");
 return false;
 }

if(form.resistance.value.length > 32)
 {
 alert("Error in input");
 return false;
 }



var Rchange = 2 * form.strain.value * form.resistance.value;


var newResistance =(parseInt(form.resistance.value) + Rchange);
  document.getElementById("newResistance").innerHTML = chopTo4(newResistance);

  }

function chopTo4(raw)
{
 strRaw = raw.toString();
 if(strRaw.length - strRaw.indexOf("0") > 4)
strRaw = strRaw.substring(0,strRaw.indexOf("0") + 5);
return strRaw;
}

</script>

</head>
<body>


<table>
 <tr><form><td>Enter Strain: </td><td><input style="text-align:right" type="text" name="strain"></td></tr>
</table>
<table>
 <tr><form><td>Enter Resistance: </td><td><input style="text-align:right" type="text" name="resistance"></td></tr>
 <tr><td colspan=2 align="center"><input type="button" value="Calculate" onClick="calc(this.form)"></td></form></tr>
<tr><td>New Resistance: </td><td id="newResistance"></td></tr>
</table>
于 2012-07-19T17:49:51.027 回答