使用jQuery显示图像
Display image using jQuery
当我拾取图像时,有时它不会显示,但当我第二次单击时,图像会显示。
jQuery
$('#morefiles').change(function (event) {
if (!(/'.(gif|jpg|jpeg|tiff|png)$/i).test($(this).val())) {
$ionicPopup.alert({
title: 'Message',
template: 'You must select an image file only !'
});
$('#morefiles').val('');
} else {
var obj = {};
obj.key = event.target.files[0];
obj.value = URL.createObjectURL(event.target.files[0]);
$scope.items.push(obj);
console.log(JSON.stringify(obj));
$('#morefiles').val(obj);
}
});
HTML
<input type="file" multiple="" id="morefiles" accept="image/*" >
如何解决这个问题?当用户选择图像时,我需要显示当时的图像。提前谢谢。
好的。我抓住你了。。添加此行:
$scope.$apply();
到您的代码如下:
$('#morefiles').change(function (event) {
if (!(/'.(gif|jpg|jpeg|tiff|png)$/i).test($(this).val())) {
$ionicPopup.alert({
title: 'Message',
template: 'You must select an image file only !'
});
$('#morefiles').val('');
} else {
var obj = {};
obj.key = event.target.files[0];
obj.value = URL.createObjectURL(event.target.files[0]);
$scope.items.push(obj);
$scope.$apply()
console.log(JSON.stringify(obj));
$('#morefiles').val(obj);
}
});
我也面临这个问题。但是通过这种方式解决了。我不知道为什么我需要做$scope.$apply();
。我没有足够的时间去挖掘这些东西。如果有人知道原因,欢迎你评论我的回答。
试试这个:
$(function() {
$('#morefiles').on('change', gotPic);
});
function gotPic(event) {
if (event.target.files.length) {
for (var i = 0; i < event.target.files.length; i++) {
if (event.target.files[i].type.indexOf('image/') == 0) {
var src = URL.createObjectURL(event.target.files[i])
$('#preview').append('<img src = ' + src + '><br>');
}
}
}
}
img {
width: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" multiple="" accept="image/*;capture=camera" id="morefiles">
<br>
<div id="preview"></div>
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- 使用javascript在Flash中加载外部图像
- 可以在这里为背景图像设置滤镜吗
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 使用 js 将背景图像设置为 HTML
- Javascript将图像src更改为淡入淡出
- 无法在本地计算机中将画布另存为图像
- 压缩phonegap中ios的图像插件
- 如何在鼠标悬停时在另一个图像上滑动图像.