简体   繁体   English

在Chrome中上传文件期间点击“取消”可清除值

[英]Hitting 'Cancel' during file upload in Chrome clears value

I'm working with a simple html type="file" input, and I'm having an issue in Chrome. 我正在处理一个简单的html type =“file”输入,我在Chrome中遇到了问题。 Specifically, when you browse to and choose a file, it saves the value. 具体来说,当您浏览并选择文件时,它会保存该值。 However, if you re-browse, then press cancel it will clear out the value. 但是,如果您重新浏览,则按取消它将清除该值。

The html is simple: html很简单:

<input type="file">

Here is a simple fiddle- http://jsfiddle.net/78ghn/ . 这是一个简单的小提琴 - http://jsfiddle.net/78ghn/

This doesn't happen in other browsers -- is there a way to force Chrome to retain the value?? 这种情况在其他浏览器中不会发生 - 是否有办法强制Chrome保留这些值?

 function f() { document.getElementById("b").appendChild(document.getElementById("a")); document.getElementById("d").innerHTML = document.getElementById("c").innerHTML document.getElementById("alert").innerHTML = 'Your last file was '.concat(document.getElementById("b").lastChild.value.slice(12)) } function g() { if(document.getElementById("b").lastChild.value) { document.write("You have submitted ".concat(document.getElementById("b").lastChild.value.slice(12))); } else { document.write("You have submitted nothing."); } } 
 #a { opacity: 0; width: 100%; height: 100%; } #c { display: none; } #d { width: 100%; height: 100%; } #e { background-color: green; border: 2px solid black; color: white; font-size: 16pt; width: 180px; height: 90px; } #f { position: relative; left: 25%; bottom: 70%; } 
 <form> <div id='e'> <span id='d'> <input type="file" onchange='f();' id='a'> </span> <span id='f'>Select File</span> </div> <input type='button' value='Submit' onclick='g();'> </form> <span id='alert'>You have chosen no files.</span> <ul id='b'> </ul> <form id='c'> <input type="file" onchange='f();' id='a'> </form> 

I was unable to find a native implementation for this, so I tried my own workaround. 我无法为此找到本机实现,所以我尝试了自己的解决方法。 It takes input from a custom CSS button overlay, then adds the actual input element to a list and replaces it with an empty one. 它从自定义CSS按钮覆盖中获取输入,然后将实际input元素添加到列表中并将其替换为空列表。 The value is read and displayed, as it would be with a normal input . 读取和显示该值,就像使用普通input It is not included, but submitting it would involve moving the original input (last element of ul with id='b' ) to a form and submitting it via JavaScript. 它不包括在内,但提交它将涉及将原始inputul最后一个元素id='b' )移动到form并通过JavaScript提交。 It is not optimal, but it does work. 它不是最佳的,但确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM