Home > Article > Web Front-end > Layui's upload component usage and upload blocking
Background: There is a button on the page. Click to pop up the upload box. Start writing the method code of the button: handle unselected files to prevent uploading; display or hide uploads by judging the number of selected files. Button;
is defined in 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(); } }
For more layui knowledge, please pay attention to the layui usage tutorial column on the PHP Chinese website.
The above is the detailed content of Layui's upload component usage and upload blocking. For more information, please follow other related articles on the PHP Chinese website!