最近在使用layui對(duì)文件上傳的測(cè)試中,發(fā)現(xiàn)上傳失敗以后,點(diǎn)擊重傳按鈕,表單頁(yè)面就會(huì)刷新,然后數(shù)據(jù)全部清空了。
仔細(xì)看了官方的文檔,obj.upload(index, file);的確是在事件中使用的。
然后直接使用官方demo代碼測(cè)試還是會(huì)出現(xiàn),點(diǎn)擊重傳會(huì)刷新表單。
html:
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;"> <legend>高級(jí)應(yīng)用:制作一個(gè)多文件列表</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(); //將每次選擇的文件追加到文件隊(duì)列
//讀取本地文件
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(''));
//單個(gè)重傳
tr.find('.demo-reload').on('click', function(){
obj.upload(index, file);
});
//刪除
tr.find('.demo-delete').on('click', function(){
delete files[index]; //刪除對(duì)應(yīng)的文件
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]; //刪除文件隊(duì)列已經(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)上找到原因是因?yàn)?lt;button type="button" class="layui-btn layui-btn-xs demo-reload layui-hide">重傳</button> 少了一個(gè)type="button"屬性。
然后我查了查資料有沒有type="button"的區(qū)別:
首先<button> 標(biāo)簽的 type 屬性有三個(gè)屬性值:
submit:該按鈕是提交按鈕(除了 Internet Explorer,該值是其他瀏覽器的默認(rèn)值)。
button:該按鈕是可點(diǎn)擊的按鈕(Internet Explorer 的默認(rèn)值)。
reset:該按鈕是重置按鈕(清除表單數(shù)據(jù))。
所以在使用chrome瀏覽器的時(shí)候,沒有填寫type屬性,默認(rèn)就是submit屬性值。
那么type="button"和type="submit"有什么區(qū)別呢?
Submit是專門用于提交表單的Button,與Button的區(qū)別主要有兩點(diǎn):
type=button 就單純是按鈕功能 。
type=submit 是發(fā)送表單。
①Submit將表單提交(form.submit())作為其onclick后的默認(rèn)事件,Button并非如此。
②表單提交時(shí),所有具有name屬性的html輸入元素(包括input標(biāo)簽、button標(biāo)簽、select標(biāo)簽等)都將作為鍵值對(duì)提交,除了Submit對(duì)象。Submit對(duì)象只有在自己被單擊后的提交中才會(huì)作為鍵值對(duì)被提交。
但是對(duì)于從事WEB UI的人應(yīng)該要注意到,使用submit來(lái)提高頁(yè)面易用性:
使用submit后,頁(yè)面支持鍵盤enter鍵操作,而很多WEB軟件設(shè)計(jì)師,可能沒有注意到submit統(tǒng)一。
用button后往往頁(yè)面不支持enter鍵了。所以需要支持enter鍵,必須要設(shè)置個(gè)submit,默認(rèn)enter鍵對(duì)頁(yè)面第一個(gè)submit進(jìn)行操作。
執(zhí)行完onClick,轉(zhuǎn)到action。可以自動(dòng)提交不需要onClick。所以說(shuō)onclick這里可以不要。
執(zhí)行完onClick,跳轉(zhuǎn)文件在 js文件里控制。提交需要onClick。
比如:
1、οnclick="form1.action='a.jsp';form1.submit();" 這樣就實(shí)現(xiàn)了submit的功能了。
講白一些,就是submit會(huì)有一個(gè)跳轉(zhuǎn),頁(yè)面會(huì)刷新;而button不會(huì)刷新,就是一個(gè)button;可以用<button type="submit/button/reset"></button>來(lái)生成按鈕,更加靈活,樣式更好控制。



2022年9月27日 上午9:27 沙發(fā)
真的是江湖救急啊
頂!!!