Styling an input type=“file” button

后端 未结 30 1670
终归单人心
终归单人心 2020-11-21 11:50

How do you style an input type=\"file\" button?

30条回答
  •  旧巷少年郎
    2020-11-21 11:57

    This is simple with jquery. To give a code example of Ryan's suggestion with a slight modification.

    Basic html:

    Be sure to set the styling on the input when you're ready: opacity: 0 You can't set display: none because it needs to be clickable. But you can position it under the "new" button or tuck in under something else with z-index if you prefer.

    Setup some jquery to click the real input when you click the image.

    $('#image_icon').click(function() {
        $('#the_real_file_input').click();
    });
    

    Now your button is working. Just cut and paste the value when changed.

    $('input[type=file]').bind('change', function() {
        var str = "";
        str = $(this).val();
        $("#filename").text(str);
    }).change();
    

    Tah dah! You may need to parse the val() to something more meaningful but you should be all set.

提交回复
热议问题