$(function (){ function format_float(num, pos) { var size = Math.pow(10, pos); return Math.round(num * size) / size; } function preview(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('.preview').attr('src', e.target.result); var KB = format_float(e.total / 1024, 2); $('.size').text("檔案大小:" + KB + " KB"); } reader.readAsDataURL(input.files[0]); } } $("body").on("change", ".upl", function (){ preview(this); }) })
<form> <input type='file' class="upl"> <div> <img class="preview" style="max-width: 150px; max-height: 150px;"> <div class="size"></div> </div> </form>