LoginSignup
6
6

More than 5 years have passed since last update.

input[type=file]をデザイン的になんとかする方法

Posted at

ボタン画像をクリックしたときに、input[type=file]をクリックしたことにする(JS)

IE の場合は、実際に form 送信された内容の内、input type=file の内容が空なのだ。
http://qiita.com/Qiita/items/c686397e4a0f4f11683d

これにははまった・・・

ボタン画像とinput[type=file]を同じサイズで重ねて、input[type=file]上にして透明にする

IEでクリックするとinput[type=file]のファイルパスが書かれるところにカーソルが残って気持ち悪い・・・

input[type=file]でlabelを使う

IE8以下でlabel内に画像があった場合、その画像をクリックしても何も起こらないのでJSで対応。
このJSはIE9以上で走らせると、キャンセルや開くを押したあとにもう一度選択画面?が立ち上がる。

if ($.browser.msie && $.browser.version < 9) {
    $('label > img').click(function () {
        $('#'+$(this).parent().attr('for')).focus().click();
    });
}
6
6
2

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