<span class="file-input">
<label>
<input type="file" name="file" class="js-upload-file"/>ファイルを選択
</label>
<span class="js-upload-filename">選択されていません</span>
<span class="fileclear js-upload-fileclear">削除</span>
</span>
body {
padding: 40px 0 0 10px;
background-color: #F8F8F8;
}
input[type="file"] {
opacity: 0;
visibility: hidden;
position: absolute;
}
label {
padding: 10px 20px;
background: #fff;
color: #3A6590;
font-weight: 600;
border: 1px solid #3A6590;
display: inline-block;
cursor: pointer;
margin-right: 16px;
&.file-selected {
opacity: .2;
pointer-events: none;
}
}
.fileclear {
display: none;
padding: 10px 17px;
background: #F8F8F8;
color: #3A6590;
font-weight: 600;
border: 1px solid #3A6590;
cursor: pointer;
margin-left: 16px;
}
.file-input {
display: flex;
align-items: center;
margin-bottom: 16px;
font-size: 14px;
}
.note {
text-indent: -14px;
padding-left: 14px;
margin-top: 12px;
}
View Compiled
$(function() {
$('.js-upload-file').on('change', function () { //ファイルが選択されたら
let file = $(this).prop('files')[0]; //ファイルの情報を代入(file.name=ファイル名/file.size=ファイルサイズ/file.type=ファイルタイプ)
$('.js-upload-filename').text(file.name); //ファイル名を出力
$('.js-upload-fileclear').show(); //クリアボタンを表示
$('.js-upload-file').parent('label').addClass('file-selected');
});
$('.js-upload-fileclear').click(function() { //クリアボタンがクリックされたら
$('.js-upload-file').val(''); //inputをリセット
$('.js-upload-filename').text('選択されていません'); //ファイル名をリセット
$('.js-upload-file').parent('label').removeClass('file-selected');
$(this).hide(); //クリアボタンを非表示
});
});
This Pen doesn't use any external CSS resources.