1. 程式人生 > >實現點選上傳檔案將檔名稱顯示在text中

實現點選上傳檔案將檔名稱顯示在text中

先上程式碼

html

<input type="file" id="file"/>
<input type="text" placeholder="file name" id="aim"/>

js

var file = $('#file'),
	aim = $('#aim');
file.on('change', function( e ){
    //e.currentTarget.files 是一個數組,如果支援多個檔案,則需要遍歷
    var name = e.currentTarget.files[0].name;
    aim.val( name );
});

效果如下


點選上傳檔案後顯示