var forms = document.querySelectorAll("form")
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener("submit", function (e) {
console.log(this);
if (!this.checkValidity()) {
e.preventDefault();
}
}, true);
}
Contact Info
name
check
控制台中观察不到this的输出
更新结果:
submit事件在填写的表单内容符合要求时才会被触发。所以我后面的 if (!this.checkValidity()
如果我将表单全部填对的话是能看到this的输出的。
@yunkehe 绑定在submit上的确能阻止跳转,我这里没有成功阻止跳转是因为我的if语句没有意义造成的
在submit事件触发时验证表单内容来阻止表单提交是为了验证在js中自定义的检测
比如
Change Your Password
Password 1:
Password 2:
var password1 = document.getElementById('password1');
var password2 = document.getElementById('password2');
var checkPas