我还在学习如何构建计算器。计算器没有工作,但我不知道它有什么问题。请帮帮我。
代码
var myDiv = document.createElement('div');
function calculatedose () {
var weight = document.paeform.weight.value
var dose1 = document.paeform.dose1.value
var strengthmg = document.paeform.strengthmg.value
var strengthml = document.paeform.strengthml.value
if (weight > 0 && dose1 > 0 && strengthmg > 0 && strengthml > 0) {
var finaldose = weight*dose1*(strengthmg/strenghml)
document.paeform.dose.value = finaldose
}
}

<form name="paeform">
Weight(kg): <input name="weight" size="10" type="text" /><br />
Dosage(mg/kg): <input name="height" size="10" type="text" /><br />
Liquid formulation(mg): <input name="strengthmg" size="10" type="text" /><br />
Liquid formulation(ml): <input name="strengthml" size="10" type="text" /><br />
<input type="button" value="Calculate Dose" onclick="calculatedose()" /><br />
Amount perdose (ml): <input name="dose" size="10" type="text" /><br />
<input type="reset" value="Reset" /></form>
&#13;
答案 0 :(得分:0)
将您的代码更改为:
var myDiv = document.createElement('div');
function calculatedose() {
var weight = document.getElementById('weight').value || "0";
var dose1 = document.getElementById('dose1').value || "0";
var strengthmg = document.getElementById('strengthmg').value || "0";
var strengthml = document.getElementById('strengthml').value || "0";
weight = parseInt(weight);
dose1 = parseInt(dose1);
strengthmg = parseInt(strengthmg);
strengthml = parseInt(strengthml);
if (weight > 0 && dose1 > 0 && strengthmg > 0 && strengthml > 0) {
var finaldose = weight * dose1 * (strengthmg / strengthml)
document.getElementById('dose').value = finaldose;
}
}
<form name="paeform">
Weight(kg): <input id="weight" size="10" type="text" /><br />
Dosage(mg/kg): <input id="dose1" size="10" type="text" /><br />
Liquid formulation(mg): <input id="strengthmg" size="10" type="text" /><br />
Liquid formulation(ml): <input id="strengthml" size="10" type="text" /><br />
<input type="button" value="Calculate Dose" onclick="calculatedose()" /><br />
Amount perdose (ml): <input id="dose" size="10" type="text" /><br />
<input type="reset" value="Reset" />
</form>