layui實(shí)現(xiàn)多圖片上傳并限制上傳的圖片數(shù)量
更新時(shí)間:2019年09月26日 15:42:15 作者:十三月呀
今天小編就為大家分享一篇layui實(shí)現(xiàn)多圖片上傳并限制上傳的圖片數(shù)量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
廢話不多說了,直接上代碼吧!
//給圖片添加刪除
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(); //將每次選擇的文件追加到文件隊(duì)列
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]; //刪除對(duì)應(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]; //刪除文件隊(duì)列已經(jīng)上傳成功的文件
return;
};
}
});
以上這篇layui實(shí)現(xiàn)多圖片上傳并限制上傳的圖片數(shù)量就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript實(shí)現(xiàn)找不同色塊的游戲
先給大家說下游戲規(guī)則:在變化數(shù)量的顏色塊里找出一個(gè)不同顏色的塊點(diǎn)擊。下面通過js代碼給大家分享找不同色塊的游戲?qū)崿F(xiàn)方法,需要的朋友參考下吧2017-07-07
微信小程序如何通過用戶授權(quán)獲取手機(jī)號(hào)(getPhoneNumber)
這篇文章主要介紹了微信小程序如何通過用戶授權(quán)獲取手機(jī)號(hào)(getPhoneNumber),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
JavaScript箭頭函數(shù)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了JavaScript箭頭函數(shù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
JavaScript中EventBus實(shí)現(xiàn)對(duì)象之間通信
這篇文章主要介紹了JavaScript中EventBus實(shí)現(xiàn)對(duì)象之間通信,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
JavaScript使用ActiveXObject訪問Access和SQL Server數(shù)據(jù)庫
這篇文章主要介紹了JavaScript使用ActiveXObject訪問Access和SQL Server數(shù)據(jù)庫,本文分別給出相應(yīng)操作代碼,需要的朋友可以參考下2015-04-04

