Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JavaScript】フォームを送信する方法

Posted at

submit()

以下のように記述するとsubmit()を使用してフォームを送信できます。

<form action="/changename">
  <label for="name">名前</label>
  <input type="text" id="name">
</form>

<button>Change name</button>
const btn = document.querySelector('button')
const form = document.querySelector('form')

btn.addEventListener('click', () => {
  form.submit()
})

ただし、submit()でフォームを送信した場合には、

  • submit イベントは発生しません
  • 制約検証は行われません

requestSubmit()

requestSubmit()を使用してもフォームを送信できます。

<form action="/changename">
  <label for="name">名前</label>
  <input type="text" id="name" required>
</form>

<button>Change name</button>
const btn = document.querySelector('button')
const form = document.querySelector('form')

btn.addEventListener('click', () => {
  form.requestSubmit()
})

requestSubmit()でフォームを送信した場合には、

  • submitイベントが発生します
  • 制約検証は行われます

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Comments

No comments

Let's comment your feelings that are more than good

1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address