我想更改在使用 input="file"
时,按钮上的默认文本为 "选择文件
"。
我该如何做?另外,正如您在图像中看到的,按钮位于文本的左侧。我该如何将它放在文本的右侧?
我想更改在使用 input="file"
时,按钮上的默认文本为 "选择文件
"。
我该如何做?另外,正如您在图像中看到的,按钮位于文本的左侧。我该如何将它放在文本的右侧?
使用label
元素的for
属性来关联表单元素input
。
<div>
<label for="files" class="btn">Select Image</label>
<input id="files" style="visibility:hidden;" type="file">
</div>
$("#files").change(function() {
filename = this.files[0].name;
console.log(filename);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label for="files" class="btn">Select Image</label>
<input id="files" style="visibility:hidden;" type="file">
</div>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<button style="display:block;width:120px; height:30px;" onclick="document.getElementById('getFile').click()">Your text here</button>
<input type='file' id="getFile" style="display:none">
</body>
</html>
tabindex="-1"
在隐藏的文件输入上跳过它而不是使用 Tab 键。 - Marcel<button type="button">
。 - undefined每个浏览器都有自己的控件呈现方式,因此您无法更改控件的文本或方向。
如果您想要一个基于HTML/CSS的解决方案而不是基于Flash或者Silverlight的解决方案,则可以尝试一些“小技巧”。
http://www.quirksmode.org/dom/inputfile.html
http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
个人认为,由于大多数用户坚持使用他们首选的浏览器,并且因此可能习惯于在默认呈现中看到控件,如果他们看到不同的东西(取决于您处理的用户类型),他们可能会感到困惑。
这可能会在将来帮助到某些人,您可以根据自己的喜好为输入标签设置样式,并在其中放置任何想要的内容,然后通过“display none”隐藏输入标签。
在 iOS 上使用 cordova 完美运行。
<link href="https://cdnjs.cloudflare.com/ajax/libs/ratchet/2.0.2/css/ratchet.css" rel="stylesheet"/>
<label for="imageUpload" class="btn btn-primary btn-block btn-outlined">Seleccionar imagenes</label>
<input type="file" id="imageUpload" accept="image/*" style="display: none">
display:none
CSS 属性隐藏默认输入按钮,并添加一个新的按钮元素来替换它,以便我们可以根据需要进行自定义。
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
Optional text here
<label for="inputField" class="btn btn-info">Try me</label>
<input type="file" id="inputField" style="display:none">
在这种情况下,添加到按钮元素的onclick
属性指示JavaScript在单击可见按钮时点击隐藏的默认输入按钮。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Optional text here
<button style="cursor:pointer" onclick="$('#inputField').click()">Click me</button>
<input type="file" id="inputField" style="display:none">
document.getElementById('btn').addEventListener('click', () => {
document.getElementById('inputField').click();
})
Optional text here
<button style="cursor:pointer" id="btn">Click me</button>
<input type="file" id="inputField" style="display:none">
不可能实现。否则您可能需要使用Silverlight或Flash上传控件。
$(document).ready(function () {
$('#choose-file').change(function () {
var i = $(this).prev('label').clone();
var file = $('#choose-file')[0].files[0].name;
$(this).prev('label').text(file);
});
});
.custom-file-upload{
background: #f7f7f7;
padding: 8px;
border: 1px solid #e3e3e3;
border-radius: 5px;
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
can you try this
<label for="choose-file" class="custom-file-upload" id="choose-file-label">
Upload Document
</label>
<input name="uploadDocument" type="file" id="choose-file"
accept=".jpg,.jpeg,.pdf,doc,docx,application/msword,.png" style="display: none;" />
技巧在于在文件输入的单击事件上触发单击事件并通过 CSS 管理默认输入文件的可见性。以下是您可以执行此操作的方法:
jQuery:
$(function() {
$("#labelfile").click(function() {
$("#imageupl").trigger('click');
});
})
css
.file {
position: absolute;
clip: rect(0px, 0px, 0px, 0px);
display: block;
}
.labelfile {
color: #333;
background-color: #fff;
display: inline-block;
margin-bottom: 0;
font-weight: 400;
text-align: center;
vertical-align: middle;
cursor: pointer;
background-image: none;
white-space: nowrap;
padding: 6px 8px;
font-size: 14px;
line-height: 1.42857143;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
HTML代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<input name="imageupl" type="file" id="imageupl" class="file" />
<label class="labelfile" id="labelfile"><i class="icon-download-alt"></i> Browse File</label>
</div>
const fileBlocks = document.querySelectorAll('.file-block')
const buttons = document.querySelectorAll('.btn-select-file')
;[...buttons].forEach(function (btn) {
btn.onclick = function () {
btn.parentElement.querySelector('input[type="file"]').click()
}
})
;[...fileBlocks].forEach(function (block) {
block.querySelector('input[type="file"]').onchange = function () {
const filename = this.files[0].name
block.querySelector('.btn-select-file').textContent = 'File selected: ' + filename
}
})
.btn-select-file {
border-radius: 20px;
}
input[type="file"] {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="file-block">
<button class="btn-select-file">Select Image 1</button>
<input type="file">
</div>
<br>
<div class="file-block">
<button class="btn-select-file">Select Image 2</button>
<input type="file">
</div>
HTML
<input type=file hidden id=choose name=choose>
<input type=button onClick=getFile.simulate() value=getFile>
<label id=selected>Nothing selected</label>
JS
var getFile = new selectFile;
getFile.targets('choose','selected');
DEMO