0
這是一個老問題,但我還沒有找到滿足我需求的解決方案。更改「選擇文件」按鈕並繼續顯示文件名
首先,我想更改爲Choose File
另一個按鈕樣式(例如,btn btn-primary
)。其次,在選擇一個文件後,我仍然希望將它的名字顯示在屏幕上,以便人們知道所選內容。
我在Chrome 53.0.2785.116
試過兩種解決方案。他們這樣做改變Choose File
文本,而其中沒有顯示在響應文件名:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function performClick(elemId) {
var elem = document.getElementById(elemId);
if(elem && document.createEvent) {
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
elem.dispatchEvent(evt);
}
}
</script>
</head>
<body>
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
<!-- http://stackoverflow.com/a/16015086/702977 -->
<a href="#" class="btn btn-primary" type="button" onclick="document.getElementById('file').click(); return false;">Browse</a> <input id="file" name="file" type="file" style="visibility: hidden; display: none;" />
<!-- http://stackoverflow.com/a/6463467/702977 -->
<!-- <a href="#" class="btn btn-primary" type="button" onclick="performClick('file');">Choose File</a><input type="file" name="file" id="file" style="position: fixed; top: -100em" />-->
<input type="submit" id="u_button" name="u_button" value="Upload the file">
</form>
</body>
</html>
這裏是JSBin
有沒有人有辦法解決嗎?我願意接受非純HTML的解決方案,以及(例如,JQuery的)...
出於安全原因,JS從不將所選文件的實際文件路徑公開給瀏覽器。 – Terry
[如何使用javascript,jquery-ajax?]更改的所選文件的完整路徑(http://stackoverflow.com/questions/15201071/how-to-get -full-path-of-selected-file-on-change-of-input-type-file-using-jav) – Terry
夥計們,我犯了一個錯誤,我想顯示'文件名'而不是'文件路徑'。 .. – SoftTimur