Mat*_* M4 50 javascript css jquery twitter-bootstrap bootstrap-4
我正在努力使用bootstrap 4文件浏览器.如果我使用自定义文件控件,我会一直看到选择文件值. https://v4-alpha.getbootstrap.com/components/forms/#file-browser
我想在选择文件后更改选择文件的值.这个值实际上隐藏在css中.custom-file-control:lang(en)::after,我不知道如何在javascript中访问和更改它.我可以像这样得到所选文件的值:
document.getElementById("exampleInputFile").value.split("\\").pop();
Run Code Online (Sandbox Code Playgroud)
不是我需要改变
.custom-file-control:lang(en)::after {
content: "Choose file...";
}
Run Code Online (Sandbox Code Playgroud)
不知何故
Zim*_*Zim 67
2018年更新
Bootstrap 4.1
现在,"选择文件..."占位符文本设置在custom-file-label:
<div class="custom-file" id="customFile" lang="es">
<input type="file" class="custom-file-input" id="exampleInputFile" aria-describedby="fileHelp">
<label class="custom-file-label" for="exampleInputFile">
Select file...
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
更改"浏览"按钮文本需要一些额外的CSS或SASS.
.custom-file-input ~ .custom-file-label::after {
content: "Button Text";
}
Run Code Online (Sandbox Code Playgroud)
https://www.codeply.com/go/gnVCj66Efp(CSS)
https://www.codeply.com/go/2Mo9OrokBQ(SASS)
还要注意语言翻译如何使用lang=""属性.
Bootstrap 4 Alpha 6
我认为这里有两个不同的问题..
<label class="custom-file" id="customFile">
<input type="file" class="custom-file-input">
<span class="custom-file-control form-control-file"></span>
</label>
Run Code Online (Sandbox Code Playgroud)
1 - 如何更改初始占位符和按钮文本
在Bootstrap 4中,初始占位符值是custom-file-control使用::after基于HTML语言的CSS伪元素设置的.初始文件按钮(实际上不是按钮,但看起来像一个按钮)是使用CSS伪::before元素设置的.这些值可以用CSS覆盖..
#customFile .custom-file-control:lang(en)::after {
content: "Select file...";
}
#customFile .custom-file-control:lang(en)::before {
content: "Click me";
}
Run Code Online (Sandbox Code Playgroud)
2 - 如何获取所选的文件名值,并更新输入以显示值.
选择文件后,可以使用JavaScript/jQuery获取值.
$('.custom-file-input').on('change',function(){
var fileName = $(this).val();
})
Run Code Online (Sandbox Code Playgroud)
但是,由于输入的占位符文本是伪元素,因此使用Js/jQuery操作此方法并不容易.但是,您可以拥有另一个CSS类,在选择文件后隐藏伪内容 ...
.custom-file-control.selected:lang(en)::after {
content: "" !important;
}
Run Code Online (Sandbox Code Playgroud)
使用jQuery .selected在.custom-file-control选择文件后切换类.这将隐藏初始占位符值.然后将文件名值放在.form-control-filespan中...
$('.custom-file-input').on('change',function(){
var fileName = $(this).val();
$(this).next('.form-control-file').addClass("selected").html(fileName);
})
Run Code Online (Sandbox Code Playgroud)
然后,您可以根据需要处理文件上载或重新选择.
Eln*_*oor 52
我就是这样解决的
HTML:
<div class="custom-file">
<input id="logo" type="file" class="custom-file-input">
<label for="logo" class="custom-file-label text-truncate">Choose file...</label>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
$('.custom-file-input').on('change', function() {
let fileName = $(this).val().split('\\').pop();
$(this).next('.custom-file-label').addClass("selected").html(fileName);
});
Run Code Online (Sandbox Code Playgroud)
注意:感谢ajax333221提到的.text-truncate类,如果所选文件名太长,将隐藏标签内的溢出.
从Bootstrap 4.3开始,您可以在label标签内更改占位符和按钮文本:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="custom-file">
<input type="file" class="custom-file-input" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile" data-browse="{Your button text}">{Your placeholder text}</label>
</div>Run Code Online (Sandbox Code Playgroud)
更改文件浏览器的语言:
作为ZimSystem提到的替代(覆盖CSS),引导文档建议更优雅的解决方案:通过在SCSS中添加语言来构建自定义引导样式在
此处阅读:https: //getbootstrap.com/docs/4.0/components/forms/#file-browser
注意:您需要在文档中正确设置lang属性才能使其正常工作
要更新文件选择的值:
您可以使用这样的内联js来执行此操作:
<label class="custom-file">
<input type="file" id="myfile" class="custom-file-input" onchange="$(this).next().after().text($(this).val().split('\\').slice(-1)[0])">
<span class="custom-file-control"></span>
</label>
Run Code Online (Sandbox Code Playgroud)
注意:该.split('\\').slice(-1)[0]部分删除C:\ fakepath \前缀
引导程序4
更多详细信息请参见 https://learncodeweb.com/snippets/browse-button-in-bootstrap-4-with-select-image-preview/
今天我需要创建一个带有多重上传文件选项的浏览按钮,以上所有片段对我来说都不好。
当我选择多个文件时,官方 Bootstrap 示例也不起作用。
我想出的这段代码也许会在将来帮助其他人。
$(document).ready(function() {
$('input[type="file"]').on("change", function() {
let filenames = [];
let files = document.getElementById("customFile").files;
if (files.length > 1) {
filenames.push("Total Files (" + files.length + ")");
} else {
for (let i in files) {
if (files.hasOwnProperty(i)) {
filenames.push(files[i].name);
}
}
}
$(this)
.next(".custom-file-label")
.html(filenames.join(","));
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container mt-5">
<h1 class="text-center">Bootstrap 4 Upload multiple files</h1>
<div class="col-sm-6 mr-auto ml-auto border p-4">
<form method="post" enctype="multipart/form-data" action="upload.php">
<div class="form-group">
<label><strong>Upload Files</strong></label>
<div class="custom-file">
<input type="file" name="files[]" multiple class="custom-file-input form-control" id="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
<div class="form-group">
<button type="submit" name="upload" value="upload" id="upload" class="btn btn-block btn-dark"><i class="fa fa-fw fa-upload"></i> Upload</button>
</div>
</form>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
这里给出了bootstrap 3 和 bootstrap 4.3.1 的工作代码示例。
https://codepen.io/mianzaid/pen/GeEbYV
| 归档时间: |
|
| 查看次数: |
87308 次 |
| 最近记录: |