1

我想知道是否有任何方法可以隐藏或更改默认标签的内容:No file chosen对于

<input type="file" id="myFileInput"/>

到目前为止,我想出的是将其长度减少一半,以便显示工具提示。

$('input:file').css('width', parseFloat($($('input:file')).css('width'))/2 );

有任何想法吗?

4

3 回答 3

7

您不能将输入文件设计更改为每个浏览器的本机。但你仍然可以模拟它,抱歉哈基:

见演示

<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');
        }
    });
});
于 2012-12-03T14:22:37.323 回答
0

Chrome 也给了我这个问题。我尝试设置各种 CSS 选择器,但似乎没有任何效果。但是,我确实通过使用 FORM 元素找到了解决方案。

  1. 命名您的 input[type=file] 元素。
  2. 命名您的表单元素并将 input[type=file] 放入其中。
  3. 制作一个跨度并将其放在表单中的输入下方。这将是你的标签。
  4. 使用 CSS 将输入的高度设置为 0px 并将不透明度设置为 0,这将使其不可见。
  5. 使跨度绝对定位到左侧 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>

我在 Chrome 和 FF 中对此进行了测试,而不是 ie,但我希望这会有所帮助。jsfiddle http://jsfiddle.net/aressler38/L5r8L/1/

于 2013-10-22T02:49:23.390 回答
0

您也可以通过这种方式进行操作,但这是一种 hack:

<input type="file" id="myFileInput" name="html" style="width: 90px;" onchange="this.style.width = '100%';"/>
于 2012-12-03T14:26:26.920 回答