LoginSignup
8
8

More than 5 years have passed since last update.

Colorboxでsubmit結果をiframe内に表示する

Last updated at Posted at 2014-03-15

Coloroxでsubmit結果を表示したい。しかもiframe内に表示したい。

submit結果を表示するだけであれば、ググると次のような書き方がけっこうヒットする。

$('form').submit(function () {
  $.post($(this).attr('action'), $(this).serialize(), function (data) {
    $.colorbox({html: data});
  }, 'html');
  return false;
});

jQueryでPOSTして、結果をhtmlプロパティを使って表示している。
でもhtmlプロパティを有効にするとiframeプロパティが効かなくなってしまうため、このやり方だとiframe内に表示することができない。

というわけで、次のようにして無理やりiframe内にsubmit結果を表示する。

$('form').submit(function () {
  $.post($(this).attr('action'), $(this).serialize(), function (data) {
    $.colorbox({
      iframe: true,
      onComplete: function () {
        setTimeout(function () {
          $('iframe.cboxIframe').contents().find('body').html(data);
        }, 200);
      }
    });
  }, 'html');
  return false;
});

iframeプロパティを有効にするとColorboxはclass="cboxIframe"を持つiframeを作るので、その中にあるbodyタグをsumit結果に書き換えている。
setTimeoutを使わないと、自分のIEとFirefoxでは上手く表示されなかった。

8
8
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
8
8