layui 上傳插件 帶預(yù)覽 非自動(dòng)上傳功能的實(shí)例(非常實(shí)用)
首先 Html部分:
<form method="post" action="?" οnsubmit="return check();" id="form"> <div class="refund-img"> <div class="refund-img-item addRefundimg" id="addImg"> <img class="proimg" src="{DT_PATH}member/images/addbanner.png"> </div> <input type="hidden" name="thumb[]" id="proImg1"> <input type="hidden" name="thumb[]" id="proImg2"> <input type="hidden" name="thumb[]" id="proImg3"> </div> <span class="refund-img-prompt">Max. 3 attachements. Max. 3MB for each image.</span> <div class="comment-btn" id="comment-btn">Submit</div> <input style="display: none;" type="submit" name="submit" id="submitForm"/> </form>
JS部分:
//添加圖片 layui.use('upload', function() { var upload = layui.upload;//得到upload對(duì)象 var frequency = 0;//記錄上傳成功的個(gè)數(shù) //多文件列表示例 var demoListView = $('.comment-imgbox.refund-img #addImg'), uploadListIns = upload.render({ //執(zhí)行實(shí)例 elem: '#addImg',//綁定文件上傳的元素 url: '../upload.php', multiple: true, number: 3,//允許上傳的數(shù)量 auto: false, bindAction: '#comment-btn',//指向一個(gè)按鈕觸發(fā)上傳 size:'3072',//尺寸 accept: "images",//指定允許上傳時(shí)校驗(yàn)的文件類型 acceptMime:'image/*',只顯示圖片文件 exts:"jpg|png|gif|jpeg",//允許后綴 drag:"false",//是否文件拖拽上傳 data:{width:400,height:400},//上傳接口的額外參數(shù) choose: function(obj) { //選擇文件后的回調(diào)函數(shù) var files = this.files = obj.pushFile(); //將每次選擇的文件追加到文件隊(duì)列 //如果圖片3個(gè),addImg隱藏 //假如項(xiàng)目只能傳3個(gè)圖片 if(Object.keys(files).length == 3){ $("#addImg").hide(); } //讀取本地文件 如果是多文件,則會(huì)遍歷。(不支持ie8/9) console.log(index); //得到文件索引console.log(file); //得到文件對(duì)象console.log(result); //得到文件base64編碼,比如圖片//obj.upload(index, file); //對(duì)上傳失敗的單個(gè)文件重新上傳,一般在某個(gè)事件中使用 obj.preview(function(index, file, result) { //obj.upload(index, file); //對(duì)上傳失敗的單個(gè)文件重新上傳,一般在某個(gè)事件中使用 var div = $(['<div id="upload-' + index + '" class="refund-img-item exist">', '<img class="proimg" src="' + result + '">', '<img src="../member/images/close.png" class="refund-img-close">', '</div>'].join('')); //刪除列表中對(duì)應(yīng)的文件 div.find('.refund-img-close').on('click', function() { delete files[index]; //刪除對(duì)應(yīng)的文件 div.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現(xiàn)同名文件不可選 $("#addImg").show(); }); demoListView.before(div); }); }, before:function(){ //obj參數(shù)包含的信息,跟 choose回調(diào)完全一致如果帶參 修改了layui js的before方法 return confirm("Did you confirm submitting this review? Comments scores and content will not be changeable after submission');"); //為了可以讓客戶在點(diǎn)擊確定是時(shí)候有2個(gè)選擇 }, done: function(res) { //上傳成功 frequency++; $("#proImg"+frequency).val(res);//隱藏域表單賦值 alert(11); //當(dāng)節(jié)點(diǎn)與上傳成功一致時(shí) if($(".refund-img .exist").length == frequency){ $("#submitForm").trigger("click");//提交表單 } }, error: function(res, index, upload) { Dtoast("Failed to upload picture"); } }) });
部分CSS:
.refund-img{ display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: flex; -webkit-box-align: center; -moz-box-align: center; -ms-flex-align: center; align-items: center; margin-top: 30px; } .refund-img-item{ width: 30%; position: relative; } .refund-img-item:nth-child(2){ margin: 0 5%; } .addRefundimg{ border: 1px dashed #BFBFBF; } .refund-img-item img.proimg{ width: 100%; } .refund-img-item input[type=file]{ position: absolute; left: 0; top: 0; width: 100%; height: 100%; outline: none; border: none; opacity: 0; } .refund-img-close{ position: absolute; width: 20px; top: 0; right: 0; padding-right: 5px; padding-top: 5px; /*display: none;*/ } .addRefundimg .refund-img-close{ /*display: none;*/ } .refund-img-prompt{ display: block; margin-top: 5px; margin-bottom: 3px; } .refund-submit{ display: block; text-align: center; height: 40px; line-height: 40px; width: 98%; background-color: #fc6900; color: #fff; font-size: 16px; border: none; outline: none; margin-top: 8px; margin-bottom: 20px; } .comment-btn{ width: 96%; background-color: #fc6900; color: #fff; height: 36px; text-align: center; line-height: 36px; display: block; outline: none; border: none; margin-top: 30px; }
我用的layui版本是layui2.2.5 它這個(gè)默認(rèn)不支持阻止圖片上傳的,所以需要改動(dòng)框架的upload.js,
改動(dòng)前 (查找before快速定位):
y=function(){return"choose"===t?l.choose&&l.choose(g):(l.before&&l.before(g),a.ie?a.ie>9?u():c():void u())};
降上面代碼稍作修改 改為以下:
if("choose"===t){return l.choose&&l.choose(g)}; if(l.before&&l.before(g)){return false}; return (a.ie?a.ie>9?u():c():void u());
以上這篇layui 上傳插件 帶預(yù)覽 非自動(dòng)上傳功能的實(shí)例(非常實(shí)用)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)layui數(shù)據(jù)表格動(dòng)態(tài)cols(字段)動(dòng)態(tài)變化詳解
今天小編就為大家分享一篇對(duì)layui數(shù)據(jù)表格動(dòng)態(tài)cols(字段)動(dòng)態(tài)變化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10JS實(shí)現(xiàn)仿FLASH效果的豎排導(dǎo)航代碼
這篇文章主要介紹了JS實(shí)現(xiàn)仿FLASH效果的豎排導(dǎo)航代碼,涉及JavaScript基于定時(shí)函數(shù)動(dòng)態(tài)設(shè)置頁面元素樣式的技巧,具有FLASH變換效果,需要的朋友可以參考下2015-09-09javascript實(shí)現(xiàn)的樣式表(CSS) 格式整理與壓縮
javascript實(shí)現(xiàn)的樣式表(CSS) 格式整理與壓縮,可以分為多行與單行,非常不錯(cuò)。2010-05-05javascript實(shí)現(xiàn)將文件保存到本地方法匯總
本文給大家匯總介紹了3中使用javascript實(shí)現(xiàn)將文件保存到本地的方法,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07javascript計(jì)時(shí)器編寫過程與實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了javascript計(jì)時(shí)器編寫過程與實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02使用JavaScript判斷一個(gè)元素是否在可視范圍內(nèi)的幾種方法
在Web開發(fā)中,有時(shí)我們需要知道一個(gè)元素是否在用戶的可視范圍內(nèi),以便執(zhí)行相應(yīng)的操作,比如延遲加載圖片、實(shí)現(xiàn)懶加載、或是觸發(fā)動(dòng)畫效果, 本文將詳細(xì)介紹使用 JavaScript 如何判斷一個(gè)元素是否在可視范圍內(nèi)的幾種方法,需要的朋友可以參考下2024-02-02淺析微信小程序自定義日歷組件及flex布局最后一行對(duì)齊問題
這篇文章主要介紹了微信小程序自定義日歷組件及flex布局最后一行對(duì)齊問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10