![](/img/trans.png)
[英]<input type=“file”> change event firing again when navigating back to the page
[英]JS- Input type file firing change evt when there is no change
嗨,我一直在使用相同的輸入來上傳文件的腳本,當我按照以下步驟操作時會出現問題:
問題是當我認為不必要時會提醒文本
碼:
<form parameters="values...">
<input type="file" id="file"/>
</form>
<script>
$(document).ready(function () {
$("#file").on("change", function () {
let input = this;
if(input.files && input.files[0]) {
//upload
}else {
alert("Error: Incompatible browser");
}
});
});
</script>
除非沒有瀏覽器兼容性,否則永遠不會發出警報,但此“ BUG”會使警報出現
這是一項Chrome 功能 ,當用戶單擊“取消”時,它實際上會清除之前設置的輸入。
IMO, “清除”按鈕會更有意義,但是由於文件拾取UI是操作系統之一,所以我想他們無法更改它。
但是無論如何,您需要檢查不兼容的瀏覽器
// in onchange
if(input.files){
甚至更好
// before assigning the change handler
if('FileList' in window){
由於不支持<input type="file">
瀏覽器既不會設置input.files
,也不會具有FileList構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.