我使用CSS设置了一个文件输入:
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}

<form>
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-cloud-upload"></i> Upload Image
</label>
<input id="file-upload" name='upload_cont_img' type="file" style="display:none;">
</form>
&#13;
一切正常,但我想显示所选的文件名。这怎么可能使用CSS或jQuery?
答案 0 :(得分:19)
您必须绑定并触发[type=file]
元素上的更改事件,并将文件名称读取为:
$('#file-upload').change(function() {
var i = $(this).prev('label').clone();
var file = $('#file-upload')[0].files[0].name;
$(this).prev('label').text(file);
});
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-cloud-upload"></i> Upload Image
</label>
<input id="file-upload" name='upload_cont_img' type="file" style="display:none;">
</form>
答案 1 :(得分:16)
输入更改时需要获取文件名,并将其插入html。在代码this.files[0].name
中获取所选文件的名称。
$("#file-upload").change(function(){
$("#file-name").text(this.files[0].name);
});
$("#file-upload").change(function(){
$("#file-name").text(this.files[0].name);
});
&#13;
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-cloud-upload"></i> Upload Image
</label>
<input id="file-upload" name='upload_cont_img' type="file" style="display:none;">
<label id="file-name"></label>
</form>
&#13;
您也可以使用纯javascript
完成这项工作document.querySelector("#file-upload").onchange = function(){
document.querySelector("#file-name").textContent = this.files[0].name;
}
document.querySelector("#file-upload").onchange = function(){
document.querySelector("#file-name").textContent = this.files[0].name;
}
&#13;
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}
&#13;
<form>
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-cloud-upload"></i> Upload Image
</label>
<input id="file-upload" name='upload_cont_img' type="file" style="display:none;">
<label id="file-name"></label>
</form>
&#13;
答案 2 :(得分:7)
您可以像这样获取文件名
$('#file-upload')[0].files[0].name
答案 3 :(得分:4)
您也可以将其用于多文件上传
updateList = function() {
var input = document.getElementById('file');
var output = document.getElementById('fileList');
output.innerHTML = '<ul>';
for (var i = 0; i < input.files.length; ++i) {
output.innerHTML += '<li>' + input.files.item(i).name + '</li>';
}
output.innerHTML += '</ul>';
}
&#13;
<input type="file" name="file" id="file" multiple
onchange="javascript:updateList()" />
<br/>Selected files:
<div id="fileList"></div>
&#13;
答案 4 :(得分:3)
仅CSS解决方案
.file_upload {
position: relative;
min-width: 90px;
text-align: center;
color: #ee3333;
line-height: 25px;
background: #fff;
border: solid 2px #ee3333;
font-weight: 600;
}
a.file_upload {
display: inline-block;
}
.file_upload .btn_lbl {
position: relative;
z-index: 2;
pointer-events: none;
}
.file_upload .btn_colorlayer {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #fff;
z-index: 1;
pointer-events: none;
}
.file_upload input[type="file"] {
position: absolute;
top: 3px;
left: -86px;
font-weight: 600;
margin-left: 100%;
color: #ee3333;
outline: none;
}
<button class="file_upload" type="button">
<span class="btn_lbl">Browse</span>
<span class="btn_colorlayer"></span>
<input type="file" name="fileupload" id="file_upload" />
</button>
答案 5 :(得分:1)
上传按钮样式
input[type="file"] {
display: none;
}
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-cloud-upload"></i> Custom Upload
</label>
<input id="file-upload" type="file"/>
答案 6 :(得分:1)
我有一个很长的裂缝我希望它可以帮助你可能需要根据自己的喜好设计它
HTML
<form>
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-cloud-upload"></i> Upload Image
</label>
<input id="file-upload" name='upload_cont_img' type="file" style="display:none;">
<input id="uploadFile" placeholder="No File" disabled="disabled" />
</form>
<强> CSS 强>
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}
#uploadFile {
text-align: center;
border: none;
background-color: white;
color: black;
}
<强>的JavaScript 强>
document.getElementById("file-upload").onchange = function() {
document.getElementById("uploadFile").value = this.value;
};
JSFiddle链接:https://jsfiddle.net/kd1brhny/