index.html
<form id="loginform">
<div class="mb-3">
<label for="username" class="form-label">用户名</label>
<input type="text" class="form-control" id="username" aria-describedby="emailHelp" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">密码</label>
<input type="password" class="form-control" id="password" required>
</div>
<button type="submit" class="btn btn-primary">登录</button>
<!-- <div id="emailHelp" class="form-text">用户名密码不匹配</div> -->
</form>
表单中的required是一个验证属性 当表单为空的时候就会提示当前表单为空 就点击不了submit按钮
index.js
const loginform = document.querySelector("#loginform")
loginform.onsubmit = function(evt) {
evt.preventDefault() // 阻止默认行为 不然点击submit按钮会默认跳转到当前页
console.log(username.value, password.value)
}
效果: