admin管理员组文章数量:1304164
I was wondering if there is any way to hide or change the content of the default label:No file chosen
for
<input type="file" id="myFileInput"/>
What I e up with so far is to decrease its length by half, so that it displays a tooltip.
$('input:file').css('width', parseFloat($($('input:file')).css('width'))/2 );
Any Ideas?
I was wondering if there is any way to hide or change the content of the default label:No file chosen
for
<input type="file" id="myFileInput"/>
What I e up with so far is to decrease its length by half, so that it displays a tooltip.
$('input:file').css('width', parseFloat($($('input:file')).css('width'))/2 );
Any Ideas?
Share Improve this question edited Dec 3, 2012 at 14:19 Mayur Birari 5,8358 gold badges35 silver badges61 bronze badges asked Dec 3, 2012 at 14:14 Akhil SekharanAkhil Sekharan 12.7k8 gold badges42 silver badges58 bronze badges 2- 2 Use a different browser… (Seriously, I really wouldn't mess with the default rendering of standard form controls to that extent). – Quentin Commented Dec 3, 2012 at 14:18
- Here here. Some of these renderings were done for good reasons, and quite a bit of the web usability issues we have nowadays can be traced back to people circumventing perfectly good default behaviors. Still, if you are required to do this for work, you may not have much of a choice. – RonaldBarzell Commented Dec 3, 2012 at 14:20
4 Answers
Reset to default 7You cannot change input file design as its native to each browser. But you still can simulate it, sorry hacky:
See DEMO
<button id="btn_myFileInput">Choose file...</button>
<label for="btn_myFileInput">No file choosen or whatever...</label>
<input type="file" id="myFileInput" multiple />
JS:
$(function () {
$('#btn_myFileInput').data('default', $('label[for=btn_myFileInput]').text()).click(function () {
$('#myFileInput').click()
});
$('#myFileInput').on('change', function () {
var files = this.files;
if (!files.length) {
$('label[for=btn_myFileInput]').text($('#btn_myFileInput').data('default'));
return;
}
$('label[for=btn_myFileInput]').empty();
for (var i = 0, l = files.length; i < l; i++) {
$('label[for=btn_myFileInput]').append(files[i].name + '\n');
}
});
});
You can also proceed in this way, but it is an hack:
<input type="file" id="myFileInput" name="html" style="width: 90px;" onchange="this.style.width = '100%';"/>
Chrome was giving me this problem too. I tried to set all sorts of CSS selectors, but nothing seemed to work well. However, I did find a solution by using the FORM element.
- name your input[type=file] element.
- name your form element and put the input[type=file] in it.
- make a span and place it below the input in the form. This will be your label.
- use CSS to set the input's height to 0px and opacity to 0, this will make it invisible.
- make the span positioned absolutely and to the left 0px.
<style> #file { height:0px; opacity:0; } #span { left:0px; position:absolute; cursor: pointer; } </style> <form name="form"> <input type="file" id="file" name="file"/> <span id="span">My File label!!!!</span> </form> <script> var span = document.getElementById("span"); span.onclick = function(event) { document.form.file.click(event); }; var span = document.getElementById("span"); span.onclick = function(event) { document.form.file.click(event); }; </script>
I tested this in Chrome and FF, not ie, but I hope this helps. jsfiddle http://jsfiddle/aressler38/L5r8L/1/
Simple style with just CSS. You HTML button will read attributes from CSS. No need javascript slowing down the page.
CSS should be as below:
.editable:empty:before {
content: attr(data-placeholder);
}
.custom-file-input::before {
content: attr(placeholder);
/* content: 'Select some files'; */
}
input button should be:
<input class="custom-file-input" placeholder="#Hashtags " bind:files id="many" multiple type="file" />
本文标签: javascriptAlter input typequotfilequot label contentStack Overflow
版权声明:本文标题:javascript - Alter input type="file" label content - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741782244a2397378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论