【发布时间】:2021-09-26 21:27:29
【问题描述】:
就像标题所说的那样简单,为什么e.PreventDefault 没有执行而所有警报都在执行?
我只希望在触发“所有详细信息匹配”警报时转发页面。 我的代码:https://jsfiddle.net/3fmru8oa/
这是我要问的部分
<script>
function loginValidator() {
const submitButton = document.getElementById("form");
const username = document.getElementById('uid')
const password = document.getElementById('pwd')
const db = new Localbase("vChatDataBase")
submitButton.addEventListener('submit', function (e) {
console.log(`Inputed field uid: ${username.value}`)
console.log(`Inputed field pwd: ${password.value}`)
db.config.debug = false
db.collection('users').doc({ username: username.value }).get().then(document => {
if(document === undefined) {
alert("No user exist's with this username!")
return e.preventDefault()
} else {
if(document['password'] !== password.value ) {
alert("Incorrect password!")
return e.preventDefault()
} else {
alert("All details matched")
return Cookies.set("cookieUsername", document['username'])
}
}
})
})
}
</script>
我尝试使用 jQuery 执行此操作,但仍然存在相同的问题。我也试过return false
这与范围界定有关吗?这是如何解决的?
【问题讨论】:
-
数据库请求是异步的。当它解决并调用
e.preventDefault()时,表单提交long 消失了。
标签: javascript html jquery forms