以下のコードは詩からは離れており、私は初心者です。これを念頭に置いて、私は<form>
内のHTMLを次ていますこのプレースホルダーの代わりに画像をアップロードするには、imgプレースホルダーをクリックしてください。
<div class="image-upload">
<img id="send_photo_img1" src="assets/images/index2.png"/>
<input id="send_photo_input1" type="file"/>
<img id="send_photo_img2" src="assets/images/index2.png"/>
<input id="send_photo_input2" type="file"/>
</div>
目標:ユーザーが画像(「+」記号の付いたプレースホルダですindex2.pngを)クリックすると、ウィンドウは、入力=ファイルからのように開きますユーザーはimgを選択し、プレースホルダーの代わりに読み込まれます。
残りのコード:
のjQuery(@Ivan Bayevのanswer hereに基づいて、それはとても良い、鉱山はかなり不器用な気圧を得た彼のコードながら...)
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#send_photo_img1').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#send_photo_img1").click(function(){
$("#send_photo_input1").trigger("click");
$("#send_photo_input1").change(function(){
readURL(this);
});
});
CSS
.image-upload > input
{
display: none;
}
これは機能します。しかし、(1)jQueryが本当に悪い、(2)最大の懸念 - プレースホルダの代わりに画像が表示されたら、私はまだ30〜40秒のような "ロード"サインを持っています。その後、それはなくなった。
アドバイスは非常に簡単です。ありがとう!