35
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

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

あまりケースとしては無いと思うけど、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();
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
35
Help us understand the problem. What are the problem?