[JS] 如何清空file input框 [整理]


测试环境:
OS --> winXP
Browsers --> IE6+, FF 3.0.11, FF 3.5, Opera 9.64, Opera 10 beta 2, Safari 4, Chrome 2.0.172

xhtml:
<input type="file" value="选择文件" id="fileInput" />
<button type="button" id="btnClear">清空文件域</button>

JS:
<script type="text/javascript">
     var btn = document.getElementById("btnClear");
     var file = document.getElementById("fileInput");
     btn.onclick = function() {
         // for IE, Opera, Safari, Chrome
         if (file.outerHTML) {
             file.outerHTML = file.outerHTML;
         } else { // FF(包括3.5)
             file.value = "";
         }
    }
</script>

posted @ 2014-02-26 12:02  天马3798  阅读(1808)  评论(0编辑  收藏  举报