【问题标题】:HTML Button not working with inline javascriptHTML 按钮不适用于内联 javascript
【发布时间】:2020-11-15 12:35:32
【问题描述】:

我创建了一个内联 javascript 来测试一个按钮,但单击时该按钮不起作用。

我认为可能是 js 代码错误,但我用一个简单的警报测试了按钮,但它仍然无法正常工作,你能帮忙吗?这是 HTML / 和一个示例 js 脚本。

function calcEquation() {
  alert("hello");
 
<form id="form-ckd" method="post">
  <div id="ckd-calc">
    <div class="card">
      <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">CKD calculator</h6>
      </div>
      <div class="card-body">
        <p>Sex of patient:</p>
        <div>
          <label class="radio-inline">
                      <input type="radio" name="sex-ckd" value="m"> Male
                    </label>
          <label class="radio-inline">
                      <input type="radio" name="sex-ckd" value="f"> Female
                    </label>
          <br /><br />
          <p>Ethnicity:</p>
          <label class="radio-inline">
                      <input type="radio" name="race-ckd" value="b"> Black
                    </label>
          <label class="radio-inline">
                      <input type="radio" name="race-ckd" value="o"> Other
                    </label>
          <br /><br />
          <p>Age of patient (years):</p>
          <input type="number" min="1" max="120" name="age-ckd" />
          <br /><br />
          <p>Serum creatinine (micromol/L):</p>
          <input type="number" min="1" max="120" name="serum-ckd" />
          <br />
        </div>
        <br />
        <hr />
        <div id="ckd-result">
          <h5 id="outs-ckd"></h5>
          <p>Glomerular Filtration Rate by the MDRD Equation: <a href="https://www.mdcalc.com/mdrd-gfr-equation#evidence" style="color:white;"><u> mdcalc website</u></a></p>
        </div>
        <button type="button" class="btn btn-primary" onclick="calcEquation();">Calculate</button>
        <button type="button" class="btn btn-danger" onclick="popup.hideEquationFormPopup();">Close</button>
        <button type="reset" class="btn btn-primary" onclick="resetCKD();">Reset</button>
      </div>
    </div>
  </div>

</form>

【问题讨论】:

  • 查看您的控制台。它告诉你出了什么问题。语法错误。
  • 另外,它是"text/javascript" 而不是text/javascript。属性必须始终用引号括起来

标签: javascript html


【解决方案1】:
  <script type=text/javascript>
        function calcEquation(){
          alert("hello");
  </script>

我想你最后忘了用} 关闭函数。

【讨论】:

    【解决方案2】:

    那是因为你错过了函数 calcEquation 上的一个闭合花括号:)。

    <script type=text/javascript>
            function calcEquation(){
              alert("hello");}
      </script>
    
    
    
    
        <form id="form-ckd" method="post">
          <div id="ckd-calc">
            <div class="card">
                <div class="card-header py-3">
                  <h6 class="m-0 font-weight-bold text-primary">CKD calculator</h6>
                </div>
                <div class="card-body">
                  <p>Sex of patient:</p>
                  <div>
                    <label class="radio-inline">
                      <input type="radio" name="sex-ckd" value="m"> Male
                    </label>
                    <label class="radio-inline">
                      <input type="radio" name="sex-ckd" value="f"> Female
                    </label>
                    <br /><br />
                    <p>Ethnicity:</p>
                    <label class="radio-inline">
                      <input type="radio" name="race-ckd" value="b"> Black
                    </label>
                    <label class="radio-inline">
                      <input type="radio" name="race-ckd" value="o"> Other
                    </label>
                    <br /><br />
                    <p>Age of patient (years):</p>
                    <input type="number" min="1" max="120" name="age-ckd" />
                    <br /><br />
                    <p>Serum creatinine (micromol/L):</p>
                    <input type="number" min="1" max="120" name="serum-ckd" />
                    <br />
                  </div>
                  <br />
                  <hr />
                  <div id="ckd-result">
                    <h5 id="outs-ckd" ></h5>
                    <p >Glomerular Filtration Rate by the MDRD Equation: <a href="https://www.mdcalc.com/mdrd-gfr-equation#evidence" style="color:white;"><u> mdcalc website</u></a></p>
                  </div>
                  <button type="button" class="btn btn-primary" onclick="calcEquation();">Calculate</button>
                  <button type="button" class="btn btn-danger" onclick="popup.hideEquationFormPopup();">Close</button>
                  <button type="reset" class="btn btn-primary" onclick="resetCKD();">Reset</button>
                </div>
              </div>
          </div>
    
        </form>

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2012-11-23
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      相关资源
      最近更新 更多