javascript - 为什么 addEventListener 对我的提交按钮不起作用?

标签 javascript html submit addeventlistener

我正处于项目的开始阶段,我对为什么我的 addEventListener 不起作用感到有些困惑。将 onClick 与提交按钮结合使用确实有效,并且会在控制台中提供输出。但是,当我尝试使其与 addEventListener 一起工作时,它不起作用。

我查看了多个不同的示例,不确定我的 HTML 或 Javascript 中是否存在错误。

<html>
  <head>
    <title>Loan Payment Estimator</title>
    <style>
      body {
        background-color: #081822;
      }
      form {
        width: 470px; margin: 0px auto;
        border:15px solid #eef5ee; background-color:  #c1cc9c; font-weight: bold; border-collapse:collapse; padding: 10px; margin-bottom: 0px;
      }
      form h2 { margin: 0; color: #244769; text-align: center;
      }
      input {
        margin:2px; width:173px;
      }
      label{
        display:inline-block;
        width:300px;
        margin-right:-180px;
        text-align:left;
      }
      .buttonSubmit {
        width: 177px;
        margin-left: 124px;
      }
    </style>
    <script>
    document.getElementById('submitbtn').addEventListener("click", payment)
    function payment() {
      principle = document.getElementById('principle').value;
      date = document.getElementById('date').value;
      length = document.getElementById('loanLength').value;
      console.log(principle);
      console.log(date);
      console.log(length);

    }

    function calcAPR() {
      aprValue = document.getElementById('loanLength').value;
      console.log(aprValue);
    }
    </script>
  </head>
  <body>
    <form>
      <h2>Loan Payment Estimator</h2>
      <label for="txtPrinciple">Principle:</label>
      <input type='text' name='txtPrinciple' id='principle' required>
      <br>
      <label for="txtDate">Date of Loan:</label>
      <input id="date" type="date" style="width:173px" required>
      <br>
      <label for="txtLength">Length of Loan:</label>
      <select style="width:173px" id="loanLength">
        <option value="12">12 Months</option>
        <option value="24">24 Months</option>
        <option value="36">36 Months</option>
        <option value="48">48 Months</option>
        <option value="60">60 Months</option>
        <option value="72">72 Months</option>
        <option value="84">84 Months</option>
      </select>
      <br>
      <label for="txtRepayment">Repayment Date:</label>
      <input type='text' name='txtRepayment' id="repayment">
      <br>
      <input type="submit" name="Submit" value="Submit" id="submitbtn" class="buttonSubmit" />
      <hr width="100%">
      <label for="txtApr">APR:</label>
      <input type='text' name='txtApr'>
      <br>
      <label for="txtInterest">Interest:</label>
      <input type='text' name='txtInterest'>
      <br>
      <label for="txtTotal">Total Paid:</label>
      <input type='text' name='txtTotal'>
      <hr width="100%">
      <h2>Trending</h2>
      <div class="row"></div>
        <img id="honda" src="2017%20Civic%20Coupe.jpg" width="150" height="110">
        <img id="audi" src="2018%20Audi%20A4.jpg" width="150" height="110">
        <img id="chevy" src="2018%20Chevy%20Bolt.jpg" width="150" height="110">
      <br>
      <div class="column">
        <img id="subaru" src="2018%20Subaru%20Impreza.jpg" width="150" height="110">
        <img id="toyota" src="2018%20Toyota%20Corolla.jpg" width="150" height="110">
        <img id="ford" src="2018%20ford%20f-150.jpeg" width="150" height="110">
      </div>
    </form>
  </body>
</html>

感谢您抽出时间来查看。我知道一旦我弄清楚了这一点,我就能完成剩下的事情。

最佳答案

页面尚未加载,因此当您查找元素时,DOM 不会显示该元素的条目。只需等待页面加载即可。

window.onload = function(){
    document.getElementById('submitbtn').addEventListener("click", payment);
};

关于javascript - 为什么 addEventListener 对我的提交按钮不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50204803/

相关文章:

javascript - 我想要相同的功能以两种方式使用react

javascript - 尝试获取数组的上限时未定义?

javascript - 自动提交不适用于此脚本

javascript - 有条件提交表格未发布

facebook - 提交需要两个 Facebook 用户的 Open Graph 操作

javascript - 为白板应用程序发送一个大数据包还是许多较小的数据包?

javascript - Dialogflow Fullfilments 响应

javascript - Google Extension 在 Google Webstore 或 Extension Gallery 上工作?

html - 如何在 Angular 2 中通过 ng-for 将 HTML 字符串转换为真正的 HTML 元素?

html - CSS 中的动态布局