h5图片上传,获取本地图片地址

说明:此处图片还未上传到服务器,点击提交才会上传到服务器

效果:

前端代码:

html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>商品评论</title>
	<meta name="viewport" content="initial-scale=1, maximum-scale=3, minimum-scale=1, user-scalable=no">
	<link rel="stylesheet" href="./comment_add.css">
	<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
	<script src="https://cdn.bootcss.com/layer/2.3/layer.js"></script>
</head>
<body>
	<header>
		<div onclick="history.back()"></div>
		<div>评价晒单</div>
		<div></div>
	</header>
	<div class="content">
		<div class="top">
			<div>
				<img src="{$goods.images}" alt="">
			</div>
			<ul class="star">
				<li class="active"></li>
				<li class="active"></li>
				<li class="active"></li>
				<li class="active"></li>
				<li class="active"></li>
			</ul>
		</div>
		<div class="top2">
			快来分享你的体验吧
		</div>
		<div class="cont">
			<textarea name="content" cols="30" rows="10" placeholder="真实的体验可以帮助更多的小伙伴哦"></textarea>
		</div>
		<div class="imgs">
			<ul>
				
			</ul>
			
			<div class="upImg">
				<input type="file" id="file" accept="image/*" />
			</div>
		</div>
	</div>
	<footer onclick="save()">
		提交
	</footer>	
</body>
</html>

css

*{
	margin:0px;
	padding:0px;
	font-size: 14px;
	font-family: '微软雅黑';
}
html,body{
	background: #F8F8F8;
}
ul{
	list-style:none;
	padding:0px;
}
img{
	width: 100%;
	height: 100%;
}
header{
	width:90px;
	height:44px;
	background:#EEEEEE;
	position: fixed;
	left:0px;
	top:0px;
	width: 100%;
}
header>div:first-child{
	float: left;
	width:44px;
	height: 44px;
	background:url('../img/back.png') no-repeat;
	background-size: ;
	background-position: center center;
}
header>div:nth-child(2){
	line-height: 44px;
	width: 50px;
	float: left;
	font-size: 12px;
	font-weight: bold;
}
.content{
	margin-top: 44px;
	padding:20px 12px;
}
.content .top{
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
	align-items: center;
}
.top>div:first-child{
	width:80px;
	height: 80px;
	overflow:hidden;
	margin-right: 12px;
}
.top ul{
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
}
.top li{
	width:30px;
	height: 30px;
	background:url('../img/star.png') no-repeat;
	background-size: 30px 30px;
	background-position: center;
	margin:0px 5px;
}
.top .active{
	background:url('../img/stared.png') no-repeat;
	background-size: 30px 30px;
	background-position: center;
}
.top2{
	margin: 12px 0px 0px 0px;
	line-height: 44px;
	height: 44px;
	color:#333;
}
.cont textarea{
	resize: none;
	border-radius: 5px;
	padding: 12px;
	outline: none;
	width: calc(100% - 24px);
	height: 100px;
}
.imgs{
	padding:12px 0px;
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
	flex-wrap: wrap;
}
.imgs ul{
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
	flex-wrap: wrap;
}
.imgs li{
	width:50px;
	height: 50px;
	overflow: hidden;
	margin-right: 6px;
	margin-bottom: 10px;
}
.upImg{
	width: 50px;
	height: 50px;
	border-radius: 5px;
	border:1px solid #ccc;
	background: url('../img/upload.png') no-repeat;
	background-size: 30px 30px;
	background-position: center;

}
#file{
	width:50px;
	height: 50px;
	opacity: 0;
}
footer{
	width:100%;
	text-align: center;
	line-height: 44px;
	height: 44px;
	position: fixed;
	bottom:0px;
	background: #FFE66D;
	font-size:16px;
	font-weight:bold;
}

最最关键的是下面的js

<script>
	var imgs=[];
	var order_id="{$Request.param.oid}";
	var goods_id="{$Request.param.gid}";
	$('.upImg').click(function(){
		len=$('.imgs li').length;
		if(len>=5){
			layer.msg('最多上传5张');
			return;
		}
		var fileTag = document.getElementById('file');
		fileTag.onchange = function () {
			var file = fileTag.files[0];
			var fileReader = new FileReader();
			fileReader.onloadend = function () {
			  	if (fileReader.readyState == fileReader.DONE) {
			  		html='<li><img src="'+fileReader.result+'"></li>';
			  		imgs.push(fileReader.result);
			  		$('.imgs ul').append(html);
			  		len=$('.imgs li').length;
			  		if(len>=5){
			  			$('#file').remove();

			  		}
			    }
			};
			console.log(fileTag.files);
			fileReader.readAsDataURL(file);
		}
	});
	$('.star li').click(function(){
		index=$(this).index();
		$('.star li').removeClass('active');
		for (var i =0; i <=index; i++){
			$('.star li').eq(i).addClass('active');
		}
	});
	function save(){
	    postData={};
		starlevel=$('.star .active').length;
		content=$.trim($('textarea').val());
		postData.starlevel=starlevel;
		postData.content=content;
		postData.images=imgs;
		postData.goods_id=goods_id;
		postData.order_id=order_id;
		index=layer.load(3,{'time':5000});
		$.post("{:url('comment/add')}",postData,function (r) {
		    layer.close(index);
			if(r.code==1){
				url="{:url('order/detail',['id'=>'pid'])}";
				url=url.replace('pid',order_id);
				location.href=url;
			}else{
				layer.msg(r.msg);
			}
		});
	}
</script>

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值