requestSubmit offers a new way to validate a form before submitting it

栏目: IT技术 · 发布时间: 5年前

内容简介:HTMLWhat remained a little bit tricky was to submit forms from within the scope of JavaScript. TheLet's assume we have the following HTML form:

HTML form elements are the foundation for the interactions in web pages. And boy, they improved quite a little bit over the last years. Developers now can use different types ( number , tel , color , ...) and set different input modes ( text , decimal , email , ...) to only name two examples.

What remained a little bit tricky was to submit forms from within the scope of JavaScript. The HTMLFormElement defines a submit method , but it does not quite behave as one would expect.

HTML's default behavior and the not matching submit method

Let's assume we have the following HTML form:

<form action="">
  <label>
    Your name
    <input type="text" required>
  </label>
  <button>Submit</button>
</form>

And some JavaScript:

document.querySelector('form')
  .addEventListener('submit', (event) => {
  // don't submit the form if it's valid
  // only log to the console
  event.preventDefault();
  console.log('submitted form');
});

When one clicks the submit button the following happens:

submit
submit

The submit event gives developers a way to react to form submissions. And it's used a lot! Common scenarios are to call preventDefault and make AJAX requests using JavaScript.

But what happens when you grab the form and submit it in JavaScript land?

The answer is – the form is submitted! ( ‍♂️ duh!) What's surprising is that there won't be an input validation , and there won't be a submit event . You would transfer all the values included in the form at the given time. This could lead to unexpected behavior, and it is not what one would expect by calling submit .

You can work around this problem by calling click on the submit button. This action triggers the standard behavior, including validation and a fired submit event.

And this approach works, great – case closed! I never thought of it as elegant or pretty, though.

A new method that does what developers expect

People started to work on a solution to this in June 2019 (the proposal is an interesting read). The HTMLFormElement now includes an extra method called requestSubmit . And this method does the same as clicking a submit button. :tada:

There is not much magic to it – it does what you expect and offers the great goodies HTML forms ship by default. I have to say – I'm excited about it!

submit requestSubmit
doesn't trigger submit event triggers submit event
doesn't trigger validation triggers validation
can't be canceled can be canceled via event.preventDefault in a submit event handler

The browser support is as follows (there is not caniuse.com entry at the time of writing:

  • :white_check_mark: Chromium browser (the new Microsoft Edge, Chrome, Opera, ...)
  • :x: Firefox (it's currently behind a flag and will ship with v75)
  • :x: Safari

You can read more about it on MDN , have a look at it in the spec or see it in action on CodePen .

You can see a #devsheet visualizing the difference in the video below.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

格蠹汇编

格蠹汇编

张银奎 / 电子工业出版社 / 2013-3-1 / 66.00元

《格蠹汇编——软件调试案例集锦》以案例形式讨论了使用调试技术解决复杂软件问题的工具和方法。全书共36章,分为四篇。前两篇每章讲述一个有代表性的真实案例,包括从堆里抢救丢失的博客,修复因误杀而瘫痪的系统,徒手战木马,拯救“发疯”的windows7,经典阅读器的经典死锁,拯救挂死的powerpoint,转储分析之双误谜团,是谁动了我的句柄,寻找系统中的“耗电大王”,解救即将被断网的系统,转储分析之系统......一起来看看 《格蠹汇编》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具