-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmr.html
117 lines (96 loc) · 2.68 KB
/
bmr.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<head>
<title>BMR 計算器</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
h1 {
text-align: center;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="number"] {
width: 100%;
padding: 8px;
font-size: 16px;
border-radius: 3px;
border: 1px solid #ccc;
box-sizing: border-box;
}
input[type="submit"] {
width: 100%;
padding: 8px;
font-size: 16px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-weight: bold;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>BMR 計算器</h1>
<form action="get" id="bmiForm">
<label for="sex">性別:</label>
<input type="radio" name="sex" id="s1" value="male" />男
<input type="radio" name="sex" id="s2" value="female" />女
<label for="height">身高(公分):</label>
<input type="number" id="height" required />
<label for="weight">體重(公斤):</label>
<input type="number" id="weight" required />
<label for="age">年齡(歲):</label>
<input type="number" id="age" required />
<input type="submit" value="計算" />
</form>
<div id="result" class="result"></div>
</div>
<script>
document
.getElementById("bmrForm")
.addEventListener("submit", function (event) {
event.preventDefault();
const height = parseInt(document.getElementById("height").value);
const weight = parseInt(document.getElementById("weight").value);
const bmr = calculateBMR(sex, height, weight, age);
showResult(bmr);
});
function calculateBMR(sex, height, weight, age) {
height = height * 1.0;
var bmr = 9.99 * weight + 6.25 * height - 4.92 * age;
if (sex === "male") {
bmr += 166 - 161;
} else if (sex === "female") {
bmr -= 161;
}
return bmr;
}
function showResult(bmr) {
const resultElement = document.getElementById("result");
alert(`BMR值為:${bmr.toFixed(2)}`);
}
</script>
</body>
</html>