码迷,mamicode.com
首页 > Web开发 > 详细

[HTML 5] preventDefault() & defaultPrevented prop

时间:2020-04-25 20:47:11      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:span   sign   app   turn   box   round   asc   input   value   

const app = document.getElementById(app);
app.innerHTML = `
  <h1>JavaScript DOM</h1>
  <form>
    <label>
      Sign-up Email
      <input type="email">
    </label>
    <label>
      I agree to the terms
      <input type="checkbox">
    </label>
  </form>
`;

const form = document.querySelector(form);
const email = form.querySelector(input[type="email"]);
const checkbox = form.querySelector(input[type="checkbox"]);

function handleSubmit(event) {
  if (!checkbox.checked) {
    event.preventDefault();
    console.log(I am not submitting...);
    console.log(event.defaultPrevented); // true
    return;
  }
  console.log(Submitted, email.value);
}

form.addEventListener(submit, handleSubmit);

 

[HTML 5] preventDefault() & defaultPrevented prop

标签:span   sign   app   turn   box   round   asc   input   value   

原文地址:https://www.cnblogs.com/Answer1215/p/12775120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!