以下代码有什么问题(我认为它与if语句有关),我试过在线查找,但没有成功?
<form action="#" method="post" name="formSeven">
<p><input type="text" name="z11" value="" size="4" /> <span> Adjusted BMI </span></p>
<p><input type="text" name="z12" value="" size="4" /> <span> Age in years </span></p>
<p><input type="text" name="z13" value="" size="4" /> <span> Male or Female </span></p>
<p><input type="text" name="result7" value="" size="4" /> <span> BF% </span></p>
<p><input onclick="calculate7()" type="BUTTON" value="Calculate" /></p>
</form>
<script type="text/javascript">//
function calculate7() {
var x = document.formSeven.z11.value;
var y = document.formSeven.z12.value;
var z = document.formSeven.z13.value;
if (z == "Female"){
document.formSeven.result7.value = x+y-5.4;
}
else if (z == "Male") {
document.formSeven.result7.value = x+y-16.2;
}
}
// ]]></script>
答案 0 :(得分:1)
最好输入性别是做出选择,因为你不必担心用户的错误:
<select id="gender" >
<option value="0" selected="selected">Female</option>
<option value="1">Male</option>
</select>
然后在你的javascript中
(parseInt(document.getElementById('gender').value) === 0) ? document.formSeven.result7.value = x+y-5.4:document.formSeven.result7.value = x+y-16.2;
答案 1 :(得分:0)
if (z == "Female")
document.formSeven.result7.value = x+y-5.4;
else if (z == "Male")
document.formSeven.result7.value = x+y-16.2;
答案 2 :(得分:0)
使用
if (z === "Female"){
document.formSeven.result7.value = x+y-5.4;
}
答案 3 :(得分:0)
这是做你想做的事的最短路。 它还为您提供了一些错误余量,因为它不区分大小写。
document.formSeven.result7.value=(z.toLowerCase()=='female'?(x+y-5.4):(x+y-16.2));
这里
document.formSeven.result7.value=(z.toLowerCase()=='female'?(x+y-5.4):(z.toLowerCase()=='male'?(x+y-16.2):''));
用你想要的任何值替换最新的z。
以下是您的完整代码:
<form action="#" method="post" name="formSeven">
<p><input type="text" name="z11" value="" size="4" /> <span> Adjusted BMI </span></p>
<p><input type="text" name="z12" value="" size="4" /> <span> Age in years </span></p>
<p><input type="text" name="z13" value="" size="4" /> <span> Male or Female </span></p>
<p><input type="text" name="result7" value="" size="4" /> <span> BF% </span></p>
<p><input onclick="calculate7()" type="BUTTON" value="Calculate" /></p>
</form>
<script type="text/javascript">//
function calculate7() {
var x = document.formSeven.z11.value;
var y = document.formSeven.z12.value;
var z = document.formSeven.z13.value.toLowerCase();
var r = document.formSeven.result7;
r.value=(z=='female'?(x+y-5.4):(z=='male'?(x+y-16.2):r.value));
}
// ]]></script>
您还可以再清理一下代码
<form action="#" method="post" name="formSeven">
<p><input type="text" name="z11" size="4" /> <span> Adjusted BMI </span></p>
<p><input type="text" name="z12" size="4" /> <span> Age in years </span></p>
<p><input type="text" name="z13" size="4" /> <span> Male or Female </span></p>
<p><input type="text" name="result7" size="4" /> <span> BF% </span></p>
<p><input onclick="calculate7()" type="button" value="Calculate" /></p>
</form>
<script type="text/javascript">
function calculate7() {
var f=document.formSeven,
x=f.z11.value,
y=f.z12.value,
z=f.z13.value.toLowerCase(),
r=f.result7;
r.value=(z=='female'?(x+y-5.4):(z=='male'?(x+y-16.2):r.value));
}
</script>
答案 4 :(得分:0)
值是字符串,因此不是添加数字,而是连接字符串。
将字符串解析为数字:
var x = parseInt(document.formSeven.z11.value, 10);
var y = parseInt(document.formSeven.z12.value, 10);