最近在使用layui對文件上傳的測試中,發(fā)現(xiàn)上傳失敗以后,點擊重傳按鈕,表單頁面就會刷新,然后數(shù)據(jù)全部清空了。
仔細看了官方的文檔,obj.upload(index, file);的確是在事件中使用的。
然后直接使用官方demo代碼測試還是會出現(xiàn),點擊重傳會刷新表單。
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>狀態(tài)</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 值,以免刪除后出現(xiàn)同名文件不可選 }); 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]; //刪除文件隊列已經(jīng)上傳成功的文件 } 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'); //顯示重傳 } });
最后網(wǎng)上找到原因是因為<button type="button" class="layui-btn layui-btn-xs demo-reload layui-hide">重傳</button> 少了一個type="button"屬性。
然后我查了查資料有沒有type="button"的區(qū)別:
首先<button> 標簽的 type 屬性有三個屬性值:
submit:該按鈕是提交按鈕(除了 Internet Explorer,該值是其他瀏覽器的默認值)。
button:該按鈕是可點擊的按鈕(Internet Explorer 的默認值)。
reset:該按鈕是重置按鈕(清除表單數(shù)據(jù))。
所以在使用chrome瀏覽器的時候,沒有填寫type屬性,默認就是submit屬性值。
那么type="button"和type="submit"有什么區(qū)別呢?
Submit是專門用于提交表單的Button,與Button的區(qū)別主要有兩點:
type=button 就單純是按鈕功能 。
type=submit 是發(fā)送表單。
①Submit將表單提交(form.submit())作為其onclick后的默認事件,Button并非如此。
②表單提交時,所有具有name屬性的html輸入元素(包括input標簽、button標簽、select標簽等)都將作為鍵值對提交,除了Submit對象。Submit對象只有在自己被單擊后的提交中才會作為鍵值對被提交。
但是對于從事WEB UI的人應該要注意到,使用submit來提高頁面易用性:
使用submit后,頁面支持鍵盤enter鍵操作,而很多WEB軟件設計師,可能沒有注意到submit統(tǒng)一。
用button后往往頁面不支持enter鍵了。所以需要支持enter鍵,必須要設置個submit,默認enter鍵對頁面第一個submit進行操作。
執(zhí)行完onClick,轉(zhuǎn)到action??梢宰詣犹峤徊恍枰猳nClick。所以說onclick這里可以不要。
執(zhí)行完onClick,跳轉(zhuǎn)文件在 js文件里控制。提交需要onClick。
比如:
1、οnclick="form1.action='a.jsp';form1.submit();" 這樣就實現(xiàn)了submit的功能了。
講白一些,就是submit會有一個跳轉(zhuǎn),頁面會刷新;而button不會刷新,就是一個button;可以用<button type="submit/button/reset"></button>來生成按鈕,更加靈活,樣式更好控制。
2022年9月27日 上午9:27 沙發(fā)
真的是江湖救急啊
頂?。?!