How to rename HTML “browse” button of an input type=file?

前端 未结 10 1451
轻奢々
轻奢々 2020-11-27 15:01

How to rename the browse button as \"Select the file\"? E.g.:


相关标签:
10条回答
  • 2020-11-27 15:39

    The input type="file" field is very tricky because it behaves differently on every browser, it can't be styled, or can be styled a little, depending on the browser again; and it is difficult to resize (depending on the browser again, it may have a minimal size that can't be overwritten).

    There are workarounds though. The best one is in my opinion this one (the result is here).

    0 讨论(0)
  • 2020-11-27 15:40

    You can do it with a simple css/jq workaround: Create a fake button which triggers the browse button that is hidden.

    HTML

    <input type="file"/>
    <button>Open</button>
    

    CSS

    input { display: none }
    

    jQuery

    $( 'button' ).click( function(e) {
        e.preventDefault(); // prevents submitting
        $( 'input' ).trigger( 'click' );
    } );
    

    demo

    0 讨论(0)
  • 2020-11-27 15:40

    No, you can't change file upload input's text. But there are some tricks to overlay an image over the button.

    0 讨论(0)
  • 2020-11-27 15:47
    <script language="JavaScript" type="text/javascript">
    function HandleBrowseClick()
    {
        var fileinput = document.getElementById("browse");
        fileinput.click();
    }
    function Handlechange()
    {
    var fileinput = document.getElementById("browse");
    var textinput = document.getElementById("filename");
    textinput.value = fileinput.value;
    }
    </script>
    
    <input type="file" id="browse" name="fileupload" style="display: none" onChange="Handlechange();"/>
    <input type="text" id="filename" readonly="true"/>
    <input type="button" value="Click to select file" id="fakeBrowse" onclick="HandleBrowseClick();"/>
    

    http://jsfiddle.net/J8ekg/

    0 讨论(0)
  • 2020-11-27 15:48

    You can also use Uploadify, which is a great jQuery upload plugin, it let's you upload multiple files, and also style the file fields easily. http://www.uploadify.com

    0 讨论(0)
  • 2020-11-27 15:50

    A bit of JavaScript will take care of it:

    <script language="JavaScript" type="text/javascript">
    function HandleBrowseClick()
    {
        var fileinput = document.getElementById("browse");
        fileinput.click();
        var textinput = document.getElementById("filename");
        textinput.value = fileinput.value;
    }
    </script>
    
    <input type="file" id="browse" name="fileupload" style="display: none"/>
    <input type="text" id="filename" readonly="true"/>
    <input type="button" value="Click to select file" id="fakeBrowse" onclick="HandleBrowseClick();"/>
    

    Not the nicest looking solution, but it works.

    0 讨论(0)
提交回复
热议问题