背景:页面上一个按钮,点击弹出上传框,从按钮的方法代码开始写:处理未选择文件阻止上传;通过判断选择文件的数量,显示或隐藏上传按钮;
在js中定义:
function uploadFile(){ layer.open({ type:1, title:'上传文件‘, area:['25%','400px'], content:'<div class="layui-form-item" style="margin-top:40px;">\ <div class="layui-input-block">\ <input class="layui-btn" type="button" id="chooseFIle" value="选择文件">\ <span></span>\ </div>\ </div>\ <div class="layui-form-item" style="margin-top:40px;">\ <div class="layui-input-block">\ <input class="layui-btn" type="button" id="uploadbtn" value="上传">\ <span></span>\ </div>\ </div>‘, btn:['关闭'], btn1:function(idx,ele){ layer.closeAll(); } }) createUpload(); } var files ; function createUpload(){ $("#uploadbtn").hide(); $("#chooseFile").next().next("span").text(""); layui.use(['upload'],function(){ var uploadInst = upload.render({ elem:'#chooseFile', url:' ', accept:'file', auto:false, multiple:true, acceptMime:'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', exts:'xls|xlsx', size:1024000, number:5, bindAction:'#uploadbtn', choose:function(obj){ files = this.files = boj.pushFile(); if(Object.keys(files).length>0){ $("#uploadbtn").show(); } obj.preview(function(index,file,result){ $("#chooseFile").siblings("span").append("<div title='"+index+"'>"+file.name+" <span onclick='deletefile(\""+index+"\")'>×</span></div>") if(index>0) {$("#uploadbtn").show() ;} }) }, allDone:function(obj){ if(obj.successful){ layer.msg(obj.total+"个文件上传成功!"); } }, error:function(){ layer.alert("上传成功!"); } }) } } function deletefile(index){ delete files[index]; $("#chooseFile").siblings("span").find("div[title="+index+"]").remove(); if(!Object.keys(files).length>0){ $("#uploadbtn").hide(); } }
以上就是layui的upload组件使用和上传阻止的详细内容,更多请关注亿速云其它相关文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。