Styling an input type=“file” button

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

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

30条回答
  •  佛祖请我去吃肉
    2020-11-21 11:57

    Multiple file solution with converted filename

    Bootstrap EXAMPLE

    HTML:

    No file selected
    No file selected

    1. JS with jQuery:

    $().ready(function($){
        $('.search-file-btn').children("input").bind('change', function() {
        var fileName = '';
        fileName = $(this).val().split("\\").slice(-1)[0];
        $(this).parent().next("span").html(fileName);
      })
    });
    

    2. JS without jQuery

    Array.prototype.forEach.call(document.getElementsByTagName('input'), function(item) {
      item.addEventListener("change", function() {
        var fileName = '';
        fileName = this.value.split("\\").slice(-1)[0];
        this.parentNode.nextElementSibling.innerHTML = fileName;
      });
    });
    

提交回复
热议问题