###参考記事
input タグで画像を選んだら即時にプレビューを表示する jQuery iPreview プラグインを公開しました。
###実現したいこと
railsでアプリケーションを作っていて、フォームのところでつまづいてしまっています。
input タグで画像を選んだら即時にプレビューを表示する jQuery iPreview プラグインを、
画像が選択された時ではなく、ページが読み込まれた時に表示されるように変更したいです。
理由は、フォームでのバリデーションに引っかかった時や、editアクションで一度投稿したものを再度編集する時に、フォームに入っている画像を表示させたいからです。
自分でもコードを読んでみたのですが、どこをいじっていいのかわからなかったので、わかる方がいらっしゃったら、教えていただけないでしょうか?
↓↓以下がそのコードです。↓↓
jquery
1/*! 2 * jquery.iPreview() - v1.0.0 3 * 4 * Depends on jQuery 5 * http://jquery.com/ 6 * 7 * Copyright 2013 Masunaga Ray, standing on the shoulders of giants 8 * http://www.msng.info/ 9 * 10 * Released under the MIT license 11 * http://opensource.org/licenses/mit-license.html 12 */ 13;(function($) { 14 'use strict'; 15 16 $.fn.iPreview = function(options) { 17 // Merge defaults to options 18 var settings = $.extend(true, {}, $.fn.iPreview.defaults, options); 19 20 return this.filter('input[type=file]').each(function() { 21 // Triggered when the file is selected or changed 22 $(this).on('change', function() { 23 var previews = $(settings.container); 24 25 // Loop in case `multiple` attribute is set 26 $.each(this.files, function(index, file) { 27 // Filter image files 28 if (file.type.match(/^image\/*/)) { 29 var reader = new FileReader(); 30 31 // Triggered when a file loading is finished 32 reader.onload = (function() { 33 // Generate a new img element 34 var preview = $('<img>').addClass(settings.class).css(settings.css) 35 .attr('src', reader.result); 36 37 if (settings.wrapper) { 38 preview = $(settings.wrapper).html(preview); 39 } 40 41 previews.append(preview); 42 }); 43 44 // Read the image file as Data URI scheme 45 reader.readAsDataURL(file); 46 } 47 }); 48 49 // Remove the previews when the input is changed to replace with new ones 50 $(this).on('change', function() { 51 previews.remove(); 52 }); 53 54 // If target is specified, previews are placed at the position to the target; 55 // if not, inserted just after the input element. 56 if (settings.target) { 57 switch (settings.position) { 58 case 'prepend': 59 previews.prependTo($(settings.target)); 60 break; 61 case 'before': 62 previews.insertBefore($(settings.target)); 63 break; 64 case 'after': 65 previews.insertAfter($(settings.target)); 66 break; 67 default: 68 previews.appendTo($(settings.target)); 69 break; 70 } 71 } else { 72 previews.insertAfter(this); 73 } 74 75 // Hide `hideOnPreview` elements and Show `showOnPreview` 76 $(settings.hideOnPreview).hide(); 77 $(settings.showOnPreview).show(); 78 79 // Reset everything on form reset 80 $(this).parents('form').on('reset', function() { 81 previews.remove(); 82 $(settings.hideOnPreview).show(); 83 $(settings.showOnPreview).hide(); 84 }); 85 }); 86 }); 87 }; 88 89 // Default setting values 90 $.fn.iPreview.defaults = { 91 // When a jQuery selector is set, previews are appended to the element; 92 // if not set, inserted after `this` input element and the "position" is omitted. 93 target: '', 94 95 // Position of the previews to the target; 96 // omitted if the "target" is not specified. 97 // (before|after|prepend|append) 98 position: 'append', 99 100 // HTML element used as the container of previews 101 container: '<div class="previews"></div>', 102 103 //HTML element used as the wrapper of each preview 104 wrapper: '', 105 106 // Class name for each thumbnail 107 class: 'thumbnail', 108 109 // Styles applied to each thumbnail 110 css: { 111 maxWidth: '300px', 112 maxHeight: '300px' 113 }, 114 115 // Selectors for elements to be hidden on preview 116 hideOnPreview: '', 117 118 // Selectors for elements to be shown on preview 119 showOnPreview: '' 120 }; 121})(jQuery);
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。