最近在使用layui对文件上传的测试中,发现上传失败以后,点击重传按钮,表单页面就会刷新,然后数据全部清空了。
仔细看了官方的文档,obj.upload(index, file);的确是在事件中使用的。
然后直接使用官方demo代码测试还是会出现,点击重传会刷新表单。
html:
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;"> <legend>高级应用:制作一个多文件列表</legend> </fieldset> <div class="layui-upload"> <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button> <div class="layui-upload-list"> <table class="layui-table"> <thead> <tr><th>文件名</th> <th>大小</th> <th>状态</th> <th>操作</th> </tr></thead> <tbody id="demoList"></tbody> </table> </div> <button type="button" class="layui-btn" id="testListAction">开始上传</button> </div>
js:
//多文件列表示例 var demoListView = $('#demoList') ,uploadListIns = upload.render({ elem: '#testList' ,url: 'https://httpbin.org/post' //改成您自己的上传接口 ,accept: 'file' ,multiple: true ,auto: false ,bindAction: '#testListAction' ,choose: function(obj){ var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列 //读取本地文件 obj.preview(function(index, file, result){ var tr = $(['<tr id="upload-'+ index +'">' ,'<td>'+ file.name +'</td>' ,'<td>'+ (file.size/1024).toFixed(1) +'kb</td>' ,'<td>等待上传</td>' ,'<td>' ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>' ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>' ,'</td>' ,'</tr>'].join('')); //单个重传 tr.find('.demo-reload').on('click', function(){ obj.upload(index, file); }); //删除 tr.find('.demo-delete').on('click', function(){ delete files[index]; //删除对应的文件 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 }); demoListView.append(tr); }); } ,done: function(res, index, upload){ if(res.files.file){ //上传成功 var tr = demoListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>'); tds.eq(3).html(''); //清空操作 return delete this.files[index]; //删除文件队列已经上传成功的文件 } this.error(index, upload); } ,error: function(index, upload){ var tr = demoListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>'); tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传 } });
最后网上找到原因是因为<button type="button" class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button> 少了一个type="button"属性。
然后我查了查资料有没有type="button"的区别:
首先<button> 标签的 type 属性有三个属性值:
submit:该按钮是提交按钮(除了 Internet Explorer,该值是其他浏览器的默认值)。
button:该按钮是可点击的按钮(Internet Explorer 的默认值)。
reset:该按钮是重置按钮(清除表单数据)。
所以在使用chrome浏览器的时候,没有填写type属性,默认就是submit属性值。
那么type="button"和type="submit"有什么区别呢?
Submit是专门用于提交表单的Button,与Button的区别主要有两点:
type=button 就单纯是按钮功能 。
type=submit 是发送表单。
①Submit将表单提交(form.submit())作为其onclick后的默认事件,Button并非如此。
②表单提交时,所有具有name属性的html输入元素(包括input标签、button标签、select标签等)都将作为键值对提交,除了Submit对象。Submit对象只有在自己被单击后的提交中才会作为键值对被提交。
但是对于从事WEB UI的人应该要注意到,使用submit来提高页面易用性:
使用submit后,页面支持键盘enter键操作,而很多WEB软件设计师,可能没有注意到submit统一。
用button后往往页面不支持enter键了。所以需要支持enter键,必须要设置个submit,默认enter键对页面第一个submit进行操作。
执行完onClick,转到action。可以自动提交不需要onClick。所以说onclick这里可以不要。
执行完onClick,跳转文件在 js文件里控制。提交需要onClick。
比如:
1、οnclick="form1.action='a.jsp';form1.submit();" 这样就实现了submit的功能了。
讲白一些,就是submit会有一个跳转,页面会刷新;而button不会刷新,就是一个button;可以用<button type="submit/button/reset"></button>来生成按钮,更加灵活,样式更好控制。
2022年9月27日 上午9:27 沙发
真的是江湖救急啊
顶!!!