【发布时间】:2018-05-06 17:53:55
【问题描述】:
我正在使用以下代码上传图片并在上传前显示预览。但我无法查看预览。有人能找到我犯错的地方吗
HTML
<form action="#" id="mainpost">
<fieldset>
<div class="section postdetails">
<div class="row form-group add-image">
<label class="col-sm-3 label-title">Photos for your ad <span>(This will be your cover photo )</span> </label>
<div class="col-sm-9">
<h5><i class="flaticon-upload" aria-hidden="true"></i>Select Files to Upload<span>You can add multiple images.</span></h5>
<div class="upload-section">
<label class="upload-image" for="img1" id="preview-img1">
<input type="file" name="img1" id="img1">
</label>
<label class="upload-image" for="img2" id="preview-img2">
<input type="file" name="img2" id="upload-image-two">
</label>
<label class="upload-image" for="img3" id="preview-img3">
<input type="file" name="img3" id="upload-image-three" >
</label>
<label class="upload-image" for="img4" id="preview-img4">
<input type="file" name="img4" id="upload-imagefour">
</label>
</div>
</div>
</div>
</div><!-- section -->
</fieldset>
</form><!-- form -->
脚本
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
imgId = '#preview-' + $(input).attr('id');
$(imgId).attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
$("form#mainform div.upload-image input[type='file']").change(function () {
readURL(this);
});
【问题讨论】:
-
你得到什么而不是错误?
-
@MuhammadOmerAslam 我没有发现任何错误
-
添加了一个答案看看是否有帮助
-
并将答案标记为正确,因为它已经解决了第一次尝试提出的问题