LoginSignup
38
35

More than 5 years have passed since last update.

jsでformを作ってsubmitさせる時の注意点

Posted at

あまりケースとしては無いと思うけど、js側でformElementを生成して、postでsubmitしたい場合の注意点。

基本形

submit.js
var form = document.createElement('form');
form.action = 'http://google.com';
form.method = 'post';
form.submit();

これでも動くのだが、IEだと動かない。

IE対応

どうやら、IEの場合HTMLの中に要素として挿入しないとsubmitしてくれないらしい。
なので、bodyElementに生成したformElementを突っ込んであげる。

ie_submit.js
var form = document.createElement('form');
form.action = 'http://google.com';
form.method = 'post';
document.body.appendChild(form);
form.submit();
38
35
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
38
35