0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

js問題bmi

Posted at
bmi

<!DOCTYPE html>
<html>
<head>
    <title>Js問題 CalcBmi</title>
</head>
<body>
    <input type="text" id="weight" placeholder="体重 (kg)" oninput="InputJuge(this)">
    <input type="text" id="height" placeholder="身長 (cm)" oninput="InputJuge(this)">
    <button onclick="calcBmi()">計算</button>
    <br>
    <input type="text" id="result" placeholder="BMI" readonly>

    <script>
        function InputJuge(elementId) {
            
            if (!/^[0-9]+$/.test(elementId.value)) {
                elementId.value = elementId.value.replace(/[^0-9.]/g, '');
                alert("半角数字を入力してください.");
            }
        }

        function calcBmi() {
            const weightValue = document.getElementById("weight").value;
            const heightValue = document.getElementById("height").value;

            const weight = parseFloat(weightValue);
            const height = parseFloat(heightValue) / 100;
            const bmi = weight / (height * height);
            document.getElementById("result").value = bmi.toFixed(2);
        }
    </script>
</body>
</html>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?