jQuery庫導入不正確,並且單擊該按鈕時,表單在BAC字段中未顯示答案。從這一點來看,答案應該相對簡單。請檢查BAC字段的顯示代碼。帶有單選按鈕和文本框的Javascript表單
<html>
<head>
<meta charset="utf-8" />
<title>bac2</title>
</head>
<body>
<p></p>
<form action="" id="bacForm">
<input type="radio" name="gender" value="male" /> Male<br /><br>
<input type="radio" name="gender" value="female" /> Female<br><br>
Weight <input type="text" name="weight" id="weight"/><br><br>
Hours <input type="text" name="hours" id="hours" /><br><br>
Drinks <input type="text" name="drinks" id="drinks"/><br><br>
<INPUT TYPE="button" value="submit" name="submit"><br><br>
BAC: <input type="text" name="bac" id="bac"/>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var gender = $('input[name=gender]:checked', '#bacForm').val();
var weight = parseInt($('#weight').val()) || 0;
var hours = parseInt($('#hours').val()) || 0;
var drinks = parseInt($('#drinks').val()) || 0;
var bac = 0;
if (gender=="male"){
bac = ((drinks * .06 * 100 * 1.055)/(weight * .68)) - (0.015 * hours);
}else if(gender=="female"){
bac = ((drinks * .06 * 100 * 1.055)/(weight * .55)) - (0.015 * hours);
}
$('#bac').val(bac);
});
</script>
</body>
</html>
您好,先生感謝您提供jQuery,我應該從現在開始使用它 – Klinetel
無法嵌入它嵌入後,當我單擊提交時,表單會自動重置任何建議Javascript控制檯說$函數是未定義的。 – Klinetel
你是否正確地包含了jQuery? –