Ter*_*ian 48 html css jquery file-upload bootstrap-4
我在Bootstrap 4中的自定义文件输入类有问题.在我选择了要上传的文件后,文件名不显示.
我用这个代码:
<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile02">
<label class="custom-file-label" for="inputGroupFile02">Choose file</label>
</div>
<div class="input-group-append">
<button class="btn btn-primary">Upload</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
知道如何解决它而不会太复杂吗?
deb*_*ebe 60
您需要使用javascript来显示所选文件的名称,如文档中所述:https://v4-alpha.getbootstrap.com/components/forms/#file-browser
在这里您可以找到解决方案:Bootstrap 4文件输入
这是你的例子的代码:
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</head>
<body>
<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile02"/>
<label class="custom-file-label" for="inputGroupFile02">Choose file</label>
</div>
<div class="input-group-append">
<button class="btn btn-primary">Upload</button>
</div>
</div>
<script>
$('#inputGroupFile02').on('change',function(){
//get the file name
var fileName = $(this).val();
//replace the "Choose a file" label
$(this).next('.custom-file-label').html(fileName);
})
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Anu*_*til 43
我使用了以下相同的内容:
<script type="application/javascript">
$('input[type="file"]').change(function(e){
var fileName = e.target.files[0].name;
$('.custom-file-label').html(fileName);
});
</script>
Run Code Online (Sandbox Code Playgroud)
VDW*_*WWD 19
Anuja Patil的答案仅适用于页面上的单个文件输入.如果有多个,则可以使用.如果multiple已启用,此片段还将显示所有文件.
<script type="text/javascript">
$('.custom-file input').change(function (e) {
var files = [];
for (var i = 0; i < $(this)[0].files.length; i++) {
files.push($(this)[0].files[i].name);
}
$(this).next('.custom-file-label').html(files.join(', '));
});
</script>
Run Code Online (Sandbox Code Playgroud)
请注意,$(this).next('.custom-file-label').html($(this)[0].files.join(', '));这不起作用.这就是为什么for需要循环的原因.
如果您从未在文件上传中使用多个文件,则可以使用这个更简单的代码段.
<script type="text/javascript">
$('.custom-file input').change(function (e) {
$(this).next('.custom-file-label').html(e.target.files[0].name);
});
</script>
Run Code Online (Sandbox Code Playgroud)
kej*_*ion 11
$(document).on('change', '.custom-file-input', function (event) {
$(this).next('.custom-file-label').html(event.target.files[0].name);
})
Run Code Online (Sandbox Code Playgroud)
世界上最好的。适用于动态创建的输入,并使用实际的文件名。
这适用于Bootstrap 4.1.3:
<script>
$("input[type=file]").change(function () {
var fieldVal = $(this).val();
// Change the node's value by removing the fake path (Chrome)
fieldVal = fieldVal.replace("C:\\fakepath\\", "");
if (fieldVal != undefined || fieldVal != "") {
$(this).next(".custom-file-label").attr('data-content', fieldVal);
$(this).next(".custom-file-label").text(fieldVal);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
小智 6
Johann-S 解决方案效果很好。(看起来他创建了很棒的插件)
Bootstrap 4.3文档示例页面使用此插件来修改文件名:https://github.com/Johann-S/bs-custom-file-input
使用Bootstrap 自定义文件输入类。将插件添加到您的项目中,并将此代码添加到页面上的脚本文件中。
$(document).ready(function () {
bsCustomFileInput.init()
})
Run Code Online (Sandbox Code Playgroud)
如果你愿意,你可以使用推荐的 Bootstrap 插件来动态化你的自定义文件输入:https : //www.npmjs.com/package/bs-custom-file-input
这个插件可以在有或没有 jQuery 的情况下使用,并且可以与 React 和 Angular 一起使用
| 归档时间: |
|
| 查看次数: |
46838 次 |
| 最近记录: |