更改输入类型=“文件”中的默认文本?
当我们使用 input="file"
时,我想更改按钮上的默认文本“Choose File
”。
我该如何执行此操作?正如您在图像中看到的,按钮位于文本的左侧。如何将其放在文本的右侧?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当我们使用 input="file"
时,我想更改按钮上的默认文本“Choose File
”。
我该如何执行此操作?正如您在图像中看到的,按钮位于文本的左侧。如何将其放在文本的右侧?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(27)
使用
for
<input
的label
的 /a> 属性。下面是获取上传文件名称的代码
Use the
for
attribute oflabel
forinput
.Below is the code to fetch name of the uploaded file
我想这就是你想要的:
I think this is what you want:
每个浏览器都有自己的控件呈现方式,因此您无法更改控件的文本或方向。
如果您想要 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
就个人而言,由于大多数用户坚持使用他们选择的浏览器,因此可能习惯于在默认呈现中查看控件,因此如果他们看到不同的内容,他们可能会感到困惑(取决于您正在处理的用户类型) 。
Each browser has it's own rendition of the control and as such you can't change either the text or the orientation of the control.
There are some "kind of" hacks you may want to try if you want an html/css solution rather than a Flash or silverlightsolution.
http://www.quirksmode.org/dom/inputfile.html
http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
Personally, because most users stick to their browser of choice, and therefore are probably used to seeing the control in the default rendition, they'd probably get confused if they saw something different (depending on the types of users you're dealing with).
这可能会对将来的某人有所帮助,您可以根据需要设置输入标签的样式,并将您想要的任何内容放入其中,并隐藏输入并不显示。
它在带有 iOS 的 cordova 上完美运行
This might help someone in the future, you can style the label for the input as you like and put anything you want inside it and hide the input with display none.
It works perfectly on cordova with iOS
为此,必须使用
display:none
CSS 属性隐藏默认输入按钮,并添加一个新的按钮元素来替换它,以便我们可以根据需要进行自定义。带引导程序
使用 jQuery
在这种情况下,添加到按钮元素的
onclick
属性指示 JavaScript 在单击可见按钮时单击隐藏的默认输入按钮。带有事件监听器的纯 JavaScript
To achieve this, the default input button must be hidden using
display:none
CSS property and a new button element is added to replace it, so we can customize as we wish.With Bootstrap
With jQuery
In this case the
onclick
attribute added to the button element is indicating to JavaScript to click on the hidden default input button whenever the visible button is clicked.Plain JavaScript with event listener
这是不可能的。否则您可能需要使用 Silverlight 或 Flash 上传控件。
It is not possible. Otherwise you may need to use Silverlight or Flash upload control.
2017 年更新:
我已经研究了如何实现这一目标。最好的解释/教程在这里:
https://tympanus.net/codrops /2015/09/15/styling-customizing-file-inputs-smart-way/
我会在这里写下摘要,以防万一它不可用。所以你应该有 HTML:
然后用 CSS 隐藏输入:
然后设置标签样式:
然后可以选择添加 JS 来显示文件名:
但实际上只需阅读教程并下载演示,它真的很棒。
Update 2017:
I have done research on how this could be achieved. And the best explanation/tutorial is here:
https://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/
I'll write summary here just in case it becomes unavailable. So you should have HTML:
Then hide the input with CSS:
Then style the label:
Then optionally you can add JS to display the name of the file:
But really just read the tutorial and download the demo, it's really good.
你可以使用这种方法,即使输入很多文件也能工作。
You can use this approach, it works even if a lot of files inputs.
诀窍是在单击文件输入时触发单击事件,并通过 CSS 管理默认输入文件的可见性。您可以这样做:
jQuery:
css
HTML 代码:
The trick is to trigger a click event on click of the file input and manage the visibility of the default input file via CSS. Here's how you can do it:
jQuery:
css
HTML code:
我制作了一个脚本并将其发布在 GitHub 上: get selectFile.js
易于使用,随意克隆。
HTML
JS
演示
jsfiddle.net/Thielicious/4oxmsy49/
I made a script and published it at GitHub: get selectFile.js
Easy to use, feel free to clone.
HTML
JS
DEMO
jsfiddle.net/Thielicious/4oxmsy49/
这应该有效:
This should work:
这是如何使用引导程序完成的,只有你应该将原始输入放在某处......idk
在 head 中删除 < br>如果你有它,因为它只是隐藏的并且无论如何它都占用空间:)
Here is how its done with bootstrap, only u should put the original input somewhere...idk
in head and delete the < br > if you have it, because its only hidden and its taking space anyway :)
使用 Bootstrap,您可以像下面的代码一样执行此操作。
Using Bootstrap you can do this thing like the below code.
我的解决方案...
HTML:
Jquery:
这太邪恶了:D
My solution...
HTML :
Jquery:
This is just evil :D
我构建了一个更容易做到这一点的脚本。
例如:
基本上,我的脚本与此非常相似 链接
代码
纯javascript,无需依赖
I build a script that can be easier to do that.
For example:
basically, my script is very similar to this link
Code
Pure javascript, no dependencies needed
Reference
好的,创建自定义输入文件的非常简单的纯 CSS 方法。
使用标签,但正如您从之前的答案中知道的那样,标签不会调用 onclick
Firefox 中的函数,可能是一个错误,但与以下内容无关。
您要做的就是设置标签的样式,使其看起来像您希望的那样,
现在只需隐藏实际的输入按钮,但您无法将其设置为
visability: hide
因此,通过设置
opacity 使其不可见: 0;
现在您可能已经注意到,我的标签上有与输入字段相同的类,那是因为我希望两者具有相同的样式,因此无论您在哪里单击标签,您实际上是在点击不可见的输入字段。
Ok so very simple pure css way of creating your custom input file.
Use labels, but as you know from previous answers, label doesn't invoke onclick
function in firefox, may be a bug but doesn't matter with the following.
What you do is style the label to look how you want it to
now simply hide the actual input button, but you cant set it to to
visability: hidden
So make in invisible by setting
opacity: 0;
now as you might have noticed i have the same class on my label as i do my input field, that is because i want the to both have the same styling, thus where ever you click on the label, you are actually clicking on the invisible input field.
我会使用
按钮
来触发输入
:快速而干净。
I'd use a
button
to trigger theinput
:Quick and clean.
下面是一个风格化的上传按钮的示例,它将读取图像、压缩它并下载结果图像。它的工作原理是隐藏实际的输入元素,然后通过一些技巧,当您单击我们的假文件上传器时,它会使用实际的输入元素弹出用于选择文件的窗口。通过使用此方法,我们可以 100% 控制文件上传器的外观,因为我们使用自己的元素而不是设置文件上传菜单的样式。如果我们想在将来添加拖放功能,它还可以让您轻松添加拖放功能。
然后我实际上创建了关于此文件上传按钮的一系列博客文章。
Stack Overflow 限制似乎阻止了代码片段实际压缩和下载文件。 此处完全相同的代码显示完整的上传/压缩/下载过程确实按预期工作。
Below is an example of a stylized upload button that will read an image, compress it, and download the resulting image. It works by hiding the actual input element, and then through some trickery we make it so that when you click on our fake file uploader it uses the actual input element to pop up the window for choosing a file. By using this method we get 100% control over how the file uploader looks since we are using our own element instead of styling the file upload menu. It also makes it easy to add drag and drop functionality in the future if we ever want to do that.
And then I actually created a series of blog posts about this file upload button.
Stack Overflow limitations seem to prevent the code snippet from actually compressing and downloading the file. The exact same code here shows that the full upload/compress/download process does actually work as intended.
此问题的答案,我修复了许多评论中所说的对他们不起作用的问题,即它没有显示用户选择了多少文件。
您可以通过显示文件名称或任何您想要做的事情来轻松改进它,但我想要的只是通知用户他们已经选择了文件。
With answers from this question, I fixed what many in coments said doesn¨t work for them which is that it's not showing how many files user chose.
You can easily improve it by showing names of files instead or whatever you wish to do but all I wanted was to inform user that they have already picked files.
您可以使用简单的按钮并
使用 jquery 和 bootstrap 隐藏输入文件:
HTML 代码
CSS :
JS :
You can use a simple button and hide input file
using jquery and bootstrap :
HTML code
CSS :
JS :
自定义输入文本(不使用 jQuery)
如果您想使用普通 JavaScript 自定义文件选择器按钮的文本,这里有一个片段:
如果您想设置文件选择器按钮的样式,我有一个解决方案:
设置输入 type="file" 按钮的样式。
Customize Input Text (without jQuery)
If you want to customize the text of the file selector button, with vanilla JavaScript, here is a snippet:
I you wanted to style the file selector button, I have a solution for that:
Styling an input type="file" button.
在 Javascript 中,您可以为输入元素指定“title”属性,例如:
In Javascript you can give the input element a 'title' attribute, for example:
<input type="file" id="upload_photo" title=""onChange={onChangeTextHandle}/>
由于浏览器安全限制,您无法直接修改它。
但是,您可以隐藏一个输入并使用所需的文本创建输入:
**请不要使用
display: none
。它不会与它一起工作。You can't directly modify it due to browser security restrictions.
But, you can hide one input and create your input with the text you need:
**Please do NOT use
display: none
. It would not work with it.纯CSS解决方案:
来源: http://tympanus.net /codrops
(您也可以使用
::file-selector-button
,但这不允许您更改其文本。)Pure CSS solution:
source: http://tympanus.net/codrops
(You could also just style the button with
::file-selector-button
, but that doesn't allow you to change its text.)这是一种解决方案,它也以与未修改的文件相同的方式显示所选文件,但可以设置按钮的文本和样式。它也不需要任何外部库/模块,它是 100% 可定制的,它甚至在标题属性中显示所选文件的完整列表,就像实际的文件输入类型一样:
这是常规样式按钮的另一种变体:
Here is a solution that also shows the selected files in the same way as the unmodified one but can have the text and style of the button be set. It also does not need any external libraries/modules, it is 100% customizable, it even displays the full list of selected files in the title property, just like the actual file input type:
Here is another variation with regularly styled buttons: