按钮的Javascript事件监听器没有响应

问题描述 投票:0回答:1

我试图创建一个登录页面,当单击登录按钮时,它会从固定的 Excel 文件中检查用户名和密码,但该按钮没有任何响应。

我尝试了所有方法,这就是我的完整想法:修复了包含多个用户的 Excel 工作表文件(密码、用户名、年龄、性别、地址和电话号码),因此当单击登录时,应该使用输入的密码和用户名输入,搜索用户,如果找到,则检查密码。

我不知道为什么即使我使用 onclick 或 Submit,我的按钮也没有响应。它甚至不发出警报。

document.getElementById('loginButton').addEventListener('click', function() {
  console.log('button clicked');

  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  var fileName = 'data.xlsx'; // Fixed file name
  /*
  fetch(fileName)
    .then(response => response.arrayBuffer())
    .then(data => {
      var workbook = XLSX.read(data, {
        type: 'array'
      });
      var firstSheet = workbook.Sheets[workbook.SheetNames[0]];
      var cellUsername = firstSheet['A1'] ? firstSheet['A1'].v : '';
      var cellPassword = firstSheet['B1'] ? firstSheet['B1'].v : '';

      if (username === cellUsername && password === cellPassword) {
        window.location.href = 'profile.html';
      } else {
        alert('Invalid username or password');
      }
    })
    .catch(error => console.error('Error fetching data:', error));
    */
});
body {
  margin: 0;
  padding: 0;
  height: 100vh;
  background-image: url('https://i.pinimg.com/564x/1f/bb/df/1fbbdf0302c54ec1274d6af7e4ebd4fc.jpg');
  /* Add your background image URL here */
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.login-container {
  background: rgba(255, 255, 255, 0.8);
  padding: 30px;
  border-radius: 10px;
  width: 300px;
  margin-left: 100px;
}

.login-container h2 {
  text-align: center;
}

.form-group {
  margin-bottom: 20px;
}

.login-container button {
  background-color: #51284A;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.6);
  color: white;
}
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login Page</title>
  <!-- Include Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>

<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-4 offset-md-1 ">
        <div class="login-container">
          <h2>SIGN IN</h2>
          <form>
            <div class="form-group">
              <label for="username">Username</label>
              <div class="input-group">
                <div class="input-group-prepend">
                  <span class="input-group-text"><i class="fas fa-user"></i></span>
                </div>
                <input type="text" class="form-control" id="username" placeholder="Enter your username">
              </div>
            </div>
            <div class="form-group">
              <label for="password">Password</label>
              <div class="input-group">
                <div class="input-group-prepend">
                  <span class="input-group-text"><i class="fas fa-lock"></i></span>
                </div>
                <input type="password" class="form-control" id="password" placeholder="Enter your password">
              </div>
            </div>
            <button type="button" id="loginButton" class="btn  btn-primary btn-block">Sign In</button>
            <p class="mt-3">Don't have an account? <a href="#">Sign Up</a></p>
          </form>
        </div>
      </div>
    </div>
  </div>

  <!-- Include Bootstrap JS and Font Awesome -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  <script src="https://kit.fontawesome.com/your-fontawesome-kit.js"></script>
  <!-- Replace with your Font Awesome kit URL -->
  <script src="script.js"></script>
</body>

javascript html css dom
1个回答
0
投票

尝试将

<button type="button">...</button>
更改为
<button type="submit">...</button>
并将事件侦听器更改为
<form>
元素而不是按钮。

<form id="loginForm">
  ...
</form>
document.getElementById("loginForm").addEventListener("submit", (e) => {
  e.preventDefault();

  // your previous code
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  var fileName = 'data.xlsx'; // Fixed file name

  fetch(fileName)
      .then(response => response.arrayBuffer())
      .then(data => {
          var workbook = XLSX.read(data, { type: 'array' });
          var firstSheet = workbook.Sheets[workbook.SheetNames[0]];
          var cellUsername = firstSheet['A1'] ? firstSheet['A1'].v : '';
          var cellPassword = firstSheet['B1'] ? firstSheet['B1'].v : '';

          if (username === cellUsername && password === cellPassword) {
              window.location.href = 'profile.html';
          } else {
              alert('Invalid username or password');
          }
      })
      .catch(error => console.error('Error fetching data:', error));
});
© www.soinside.com 2019 - 2024. All rights reserved.