layui實現(xiàn)多圖片上傳并限制上傳的圖片數(shù)量
更新時間:2019年09月26日 15:42:15 作者:十三月呀
今天小編就為大家分享一篇layui實現(xiàn)多圖片上傳并限制上傳的圖片數(shù)量,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
廢話不多說了,直接上代碼吧!
//給圖片添加刪除 function mouseChange() { $(document).on("mouseenter mouseleave", ".file-iteme", function (event) { if (event.type === "mouseenter") { //鼠標(biāo)懸浮 $(this).children(".info").fadeIn("fast"); $(this).children(".handle").fadeIn("fast"); } else if (event.type === "mouseleave") { //鼠標(biāo)離開 $(this).children(".info").hide(); $(this).children(".handle").hide(); } }); } mouseChange(); //json的length function getJsonLength(jsonData){ var jsonLength = 0; for(var item in jsonData){ jsonLength++; } return jsonLength; } //多圖片上傳 var ImgList = $('#uploader-list'),uploadListIns =upload.render({ elem: '#chooseImg', url: $("#projectUrl").val()+ '/img/imgUpload.do', accept: 'images', acceptMime: 'image/jpg,image/png,image/jpeg', exts: 'jpg|png|jpeg', size: 1024, multiple: true, auto: true, choose: function(obj){ var files = obj.pushFile(); //將每次選擇的文件追加到文件隊列 var len = getJsonLength(files); //讀取本地文件 obj.preview(function (index, file, result) { if (parseInt(len)+parseInt(coachPicsArray.length)-count > 6){ layer.msg("門店圖片不能超過6張"); //遍歷 $.each(files,function(_key){ var key = _key; var value = files[_key]; if(_key == index) { //刪除 delete files[_key]; } }); }else { var reader = new FileReader(); reader.onload = function (e) { var image = new Image(); image.onload = function () { var realWidth = image.width; var realHeight = image.height; var tr = $(['<div id="upload-' + index + '" class="file-iteme">' + '<div class="removeIcon handle"> <i class="layui-icon" style="color: white ;margin-right: 40%"></i></div>' + '<img style="color: white;width: 120px" "showBig(this)" src=' + result + ' id="img-' + index + '">' + '</div>'].join('')); //刪除 tr.find('.handle').on('click', function () { $(this).parent().remove(); delete files[index]; //刪除對應(yīng)的文件 var value = $("#clubCoachPics").val().split(","); var arr = []; for( var i in value){ if (value[i] != $(this).next().data('value')){ arr.push(value[i]); } } $("#clubCoachPics").val(arr.join(",")) ; }); ImgList.append(tr); }; image.src = result; }; //正式讀取文件 reader.readAsDataURL(file); } }); }, before: function (obj) { layer.msg('圖片上傳中...', { icon: 16, shade: 0.01, time: 3000 }) }, done: function (res, index, upload) { $("#img-"+ index + "").attr("data-value",res.imgUrl); if(res.code == 0){ //上傳成功 var imgUrl = $("#clubCoachPics").val(); if(imgUrl==null||imgUrl==""){ $("#clubCoachPics").val(res.imgUrl); }else{ $("#clubCoachPics").val(imgUrl+","+res.imgUrl); } delete files[index]; //刪除文件隊列已經(jīng)上傳成功的文件 return; }; } });
以上這篇layui實現(xiàn)多圖片上傳并限制上傳的圖片數(shù)量就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
購物車前端開發(fā)(jQuery和bootstrap3)
針對購物車的操作,進行產(chǎn)品數(shù)量的增加減少,刪除購物車中產(chǎn)品項,本文使用JQuery1.11和bootstrap3進行購物車開發(fā),感興趣的小伙伴們2016-08-08JS腳本實現(xiàn)定時到網(wǎng)站上簽到/簽退功能
這篇文章主要介紹了JS腳本實現(xiàn)定時到網(wǎng)站上簽到/簽退功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04