首页 > 解决方案 > if-else 语句在 javascript/html 中不起作用

问题描述

我是 js 新手,任何帮助都将不胜感激。我创建了一个 javascript 函数,它从 HTML 表单中获取一些输入值,如下所示;

但是,在运行代码后,当我在单选按钮中选择女性时,javascript if 语句的 else 部分没有执行。我对男性的价值相同。我还创建了一个重置​​按钮,认为这可能会有所帮助,但仍然无法正常工作。请帮忙

  <script type=text/javascript>
        function calcCreatine(){
            var sexInput = document.getElementsByName("sex")[0].value;
            var ageInput = document.getElementsByName("patients-age")[0].value;
            var weightInput = document.getElementsByName("patients-weight")[0].value;
            var serumInput = document.getElementsByName("patients-serum")[0].value;
            var result;

            if (sexInput === "m") {
                result = Math.round(((140 - ageInput) * weightInput *  1.23) / serumInput);
            } else {
                result = Math.round(((140 - ageInput) * weightInput *  1.04) / serumInput);
            }
            return alert(result + " mL/min - Creatinine clearance, original Cockcroft-Gault");
        }
  </script>





  <form id="form-id">
  <div id="creat-calc">
    <div class="card">
        <div class="card-header py-3">
          <h6 class="m-0 font-weight-bold text-primary">Creatinine clearance calculator</h6>
        </div>
        <div class="card-body">
          <p>Sex of patient:</p>
          <div>
            <label class="radio-inline">
              <input type="radio" name="sex" value="m"> Male
            </label>
            <label class="radio-inline">
              <input type="radio" name="sex" value="f"> Female
            </label>
            <p>Age of patient (years):</p>
            <input type="number" min="1" max="120" name="patients-age" />
            <br /><br />
            <p>Weight of patient (kg):</p>
            <input type="number" min="1" max="120" name="patients-weight" />
            <br /><br />
            <p>Serum creatinine (micromol/L):</p>
            <input type="number" min="1" max="120" name="patients-serum" />
            <br />
          </div>
          <br />
          <hr />
          <button type="button" class="btn btn-primary" onclick="calcCreatine();">Calculate</button>
          <button type="button" class="btn btn-danger" onclick="popup.hideCeatCalcFormPopup();">Close</button>
          <button type="button" class="btn btn-primary" onclick="myFunction()">Reset</button>
        </div>
      </div>
  </div>
</form>

标签: javascripthtmldom

解决方案


试试这个

<script type=text/javascript>
                function calcCreatine(){
                    var sexInput = document.querySelector('input[name="sex"]:checked').value;
    ;
                    var ageInput = document.getElementsByName("patients-age")[0].value;
                    var weightInput = document.getElementsByName("patients-weight")[0].value;
                    var serumInput = document.getElementsByName("patients-serum")[0].value;
                    var result;
                    console.log('sexInput', sexInput)
                    if (sexInput === 'm') {
                        result = Math.round(((140 - ageInput) * weightInput *  1.23) / serumInput);
                    } else {
                        result = Math.round(((140 - ageInput) * weightInput *  1.04) / serumInput);
                    }
                    return alert(result + " mL/min - Creatinine clearance, original Cockcroft-Gault");
                }
          </script>





          <form id="form-id">
          <div id="creat-calc">
            <div class="card">
                <div class="card-header py-3">
                  <h6 class="m-0 font-weight-bold text-primary">Creatinine clearance calculator</h6>
                </div>
                <div class="card-body">
                  <p>Sex of patient:</p>
                  <div>
                    <label class="radio-inline">
                      <input type="radio" name="sex" value="m"> Male
                    </label>
                    <label class="radio-inline">
                      <input type="radio" name="sex" value="f"> Female
                    </label>
                    <p>Age of patient (years):</p>
                    <input type="number" min="1" max="120" name="patients-age" />
                    <br /><br />
                    <p>Weight of patient (kg):</p>
                    <input type="number" min="1" max="120" name="patients-weight" />
                    <br /><br />
                    <p>Serum creatinine (micromol/L):</p>
                    <input type="number" min="1" max="120" name="patients-serum" />
                    <br />
                  </div>
                  <br />
                  <hr />
                  <button type="button" class="btn btn-primary" onclick="calcCreatine();">Calculate</button>
                  <button type="button" class="btn btn-danger" onclick="popup.hideCeatCalcFormPopup();">Close</button>
                  <button type="button" class="btn btn-primary" onclick="myFunction()">Reset</button>
                </div>
              </div>
          </div>
        </form>


推荐阅读