<img id="preview3" src="img/q_jc_ry_shangchuan.png"> <input class="fileweb" type="file" id = "uploadFile" /></div>
$("#uploadFile").change(function(){ var name = this.value; var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase(); //判断上传文件格式 if(fileName !="jpg" && fileName !="jpeg" && fileName !="png"&&fileName !="gif"&&fileName !="bmp"){ //layer.alert("请选择图片格式文件上传(jpg,png,jpeg)!"); layer.msg("请选择图片格式文件上传(jpg,png,jpeg,gif,bmp)!",{time:2000}); $("#uploadFile").val(""); $("#preview3").attr("src","img/q_jc_ry_shangchuan.png"); return false; //阻止submit提交 } if(this.files[0].size>2*1024*1024){ layer.msg("图片不能大于2M",{time:2000}); $("#uploadFile").val(""); $("#preview3").attr("src","img/q_jc_ry_shangchuan.png"); return false; //阻止submit提交 } $("#preview3").attr("src",getObjectURL(this.files[0])); }); function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { // basic url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file) ; } return url ; }