-3

以下代码有什么问题(我认为它与 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>
4

5 回答 5

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;
于 2013-06-07T07:43:22.697 回答
0

利用

if (z === "Female"){
document.formSeven.result7.value = x+y-5.4;
}
于 2013-06-07T07:29:40.980 回答
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>
于 2013-06-07T07:30:22.470 回答
0

这些值是字符串,因此您将连接字符串而不是添加数字。

将字符串解析为数字:

var x = parseInt(document.formSeven.z11.value, 10);
var y = parseInt(document.formSeven.z12.value, 10);
于 2013-06-07T07:39:12.627 回答
0
if (z == "Female")
 document.formSeven.result7.value = x+y-5.4;  
else if (z == "Male")
 document.formSeven.result7.value = x+y-16.2;
于 2013-06-07T07:27:25.030 回答