使用javascript

时间:2019-05-26 15:35:34

标签: javascript

JavaScript代码不起作用。

function validateForm() {
  var fname = /^[a-z A-Z]+[a-z A-Z]+$/;
  var name = document.getElementById('fullname').value;
  var height = document.getElementById('height').value;
  var weight = document.getElementById('weight').value;
  if (!fname.test(name)) {
    alert('Please enter a valid name');
    return;
  }
  if (weight == "") {
    alert('Please enter weight');
    return;
  }
  if (weight < 25) {
    alert('Your weight must be above 25kg');
    return;
  }
  if (weight > 150) {
    alert('Your weight must be below 150kg');
    return;
  }
  if (height < 120) {
    alert('Your height must be above 120cm');
    return;
  }
  if (height > 350) {
    alert('Your height must be below 350cm');
    return;
  }
}

function bmi() {
  var height = document.getElementById('height').value;
  var weight = document.getElementById('weight').value;
  var bmi = weight / (height * height);
  document.getElementById("bmi").innerText = bmi;
}
<div class="div5">
  <section>
    <article>
      <div class="div2">
        <div class="div1">
          <h3> BMI Calculator</h3>
          <form name="myForm" action="/action-page.php" onsubmit="return       validateForm()" method="post">
            <div class="div4">
              <div class="container">
                <label for="fullname">Fullname</label>
                <input type="text" size="40" name="fullname" id="fullname" required>
              </div>
            </div>
            <div class="div3">
              <div class="container">
                <label for="in">Age</label>
                <input type="text" size="5" name="age" id="age" required>
              </div>
            </div>
            <div class="div4">
              <div class="container">
                <label for="height">Height in Centimeter</label>
                <input type="text" size="5" name="height" id="height" required>
              </div>
            </div>
            <div class="div3">
              <div class="container">
                <label for="weight">Weight in Kilogram</label>
                <input type="text" size="5" name="weight" id="weight" required>
              </div>
            </div>
            <div class="div4">
              <div class="container">
                <label for="bmi">Body mass index</label>
                <input type="text" size="5" name="bmi" id="bmi" readonly>
              </div>
            </div>
            <div class="div4">
              <input type="submit" name="calculatebmi" value="Calculate BMI" id="cal" onclick="bmi()" align="center">
            </div>
          </form>
        </div>
      </div>
    </article>
  </section>
</div>

功能bmi()不起作用 我使用onclick()来调用函数 但结果未显示在名为bmi的文本框中 validateForm()正在运行 在计算bmi并将其显示在文本框中时需要帮助 我创建了一个外部javascript文件myScript.js

0 个答案:

没有答案