submit,监听如何触发
来源:4-13 html5默认气泡修改演示
崔浩晟
2020-03-10 13:33:09
form.addEventListener("submit",function(event)里面的东西如何触发?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>气泡样式修改</title>
<style>
.oneLine{
line-height: 1.5;
margin: 10px auto;
}
.oneLine label{
width: 100px;
text-indent: 15px;
font-size: 14px;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
display: inline-block;
}
.oneLine .sinput{
width: 60%;
height: 30px;
border-radius: 6px;
border: 1px solid #e2e2e2;
}
input[type="submit"]{
margin-left: 20px;
width: 80px;
height: 30px;
border: 0;
background-color: #5899d0;
color: #ffffff;
font-size: 14px;
border-radius: 6px;
}
.error-message{
color: red;
font-size: 12px;
text-indent: 108px;
}
</style>
</head>
<body>
<form id="forms" action="">
<div class="oneLine">
<label for="name">用户名:</label>
<input autocomplete="off" type="text" id="name" name="name" class="sinput" required>
</div>
<div class="oneLine">
<label for="email">Email:</label>
<input type="email" id="email" name="email" class="sinput" required>
</div>
<div class="oneLine">
<input type="submit" value="提交" id="thesubmit">
</div>
</form>
</body>
<script>
var form = document.getElementById("forms")
replaceValidationUI(form)
function replaceValidationUI(form){
form.addEventListener("invalid",function(event){
console.log("aa")
event.preventDefault();
},true)
form.addEventListener("submit",function(event){
console.log("bb")
console.log(this.checkValidity())
if(!this.checkValidity()){
event.preventDefault();
}
},true)
}
</script>
</html>
1回答
好帮手慕慕子
2020-03-10
同学你好, 在提交表单时,触发submit的事件函数。
因为提交表单后默认会刷新页面,可以添加alert方法,在数显页面前先弹出信息,测试下查看效果,如下:
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
相似问题