Created
April 23, 2022 05:09
-
-
Save ramzs/429116dd3bab58dd73d610de0bcda08f to your computer and use it in GitHub Desktop.
Поле загрузки нескольких файлов с кнопкой изменить и кнопкой удалить. Input type file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.attach | |
position: relative | |
display: flex | |
flex-direction: column | |
&__item | |
display: flex | |
align-items: center | |
margin-bottom: 5px | |
&:last-of-type | |
margin-bottom: 0px | |
border-bottom: none | |
&--attached | |
display: flex | |
margin: 2px 0 | |
padding-left: 31px | |
.attach__up | |
display: none | |
.attach__edit, .attach__delete | |
display: inline-block | |
font-size: 11px | |
&__up | |
position: relative | |
padding-left: 31px | |
font-size: 13px | |
font-weight: 300 | |
cursor: pointer | |
&::before | |
content: '' | |
position: absolute | |
top: -3px | |
left: 0 | |
width: 22px | |
height: 21px | |
background: url(../images/dist/upload.svg) center no-repeat | |
background-size: contain | |
&__input | |
display: none | |
&__name | |
max-width: 150px | |
font-size: 11px | |
font-weight: 300 | |
white-space: nowrap | |
overflow: hidden | |
text-overflow: ellipsis | |
&__edit, &__delete | |
display: none | |
margin-left: 10px | |
cursor: pointer | |
&__edit | |
color: #2f7dfe | |
&__delete | |
width: 10px | |
height: 10px | |
background: url(../images/dist/delete.svg) center no-repeat | |
background-size: contain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.attach').each(function () { // на случай, если таких групп файлов будет больше одной | |
let attach = $(this), | |
fieldClass = 'attach__item', // класс поля | |
attachedClass = 'attach__item--attached', // класс поля с файлом | |
fields = attach.find('.' + fieldClass).length, // начальное кол-во полей | |
fieldsAttached = 0; // начальное кол-во полей с файлами | |
let newItem = '<div class="attach__item"><label><div class="attach__up">Добавить файл</div><input class="attach__input" type="file" name="files[]" /></label><div class="attach__name"></div><!--<div class="attach__edit">Изменить</div>--><div class="attach__delete"></div></div>'; // разметка нового поля | |
// При изменении инпута | |
attach.on('change', '.attach__input', function (e) { | |
let item = $(this).closest('.' + fieldClass), | |
fileName = ''; | |
if (e.target.value) { // если value инпута не пустое | |
fileName = e.target.value.split('\\').pop(); // оставляем только имя файла и записываем в переменную | |
} | |
if (fileName) { // если имя файла не пустое | |
item.find('.attach__name').text(fileName); // подставляем в поле имя файла | |
if (!item.hasClass(attachedClass)) { // если в поле до этого не было файла | |
item.addClass(attachedClass); // отмечаем поле классом | |
fieldsAttached++; | |
} | |
if (fields < 10 && fields == fieldsAttached) { // если полей меньше 10 и кол-во полей равно | |
item.before($(newItem)); // добавляем новое поле | |
fields++; | |
} | |
} else { // если имя файла пустое | |
if (fields == fieldsAttached + 1) { | |
item.remove(); // удаляем поле | |
fields--; | |
} else { | |
item.replaceWith($(newItem)); // заменяем поле на "чистое" | |
} | |
fieldsAttached--; | |
if (fields == 1) { // если поле осталось одно | |
attach.find('.attach__up').text('Загрузить файл'); // меняем текст | |
} | |
} | |
}); | |
// При нажатии на "Изменить" | |
// attach.on('click', '.attach__edit', function () { | |
// $(this).closest('.attach__item').find('.attach__input').trigger('click'); // имитируем клик на инпут | |
// }); | |
// При нажатии на "Удалить" | |
attach.on('click', '.attach__delete', function () { | |
let item = $(this).closest('.' + fieldClass); | |
if (fields > fieldsAttached) { // если полей больше, чем загруженных файлов | |
item.remove(); // удаляем поле | |
fields--; | |
} else { // если равно | |
item.before($(newItem)); // добавляем новое поле | |
item.remove(); // удаляем старое | |
} | |
fieldsAttached--; | |
if (fields == 1) { // если поле осталось одно | |
attach.find('.attach__up').text('Загрузить файл'); // меняем текст | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="attach"> | |
<div class="attach__item"> | |
<label> | |
<div class="attach__up">Загрузить файл</div> | |
<input class="attach__input" type="file" name="files" /> | |
</label> | |
<div class="attach__name"></div> | |
<!-- <div class="attach__edit">Изменить</div> --> | |
<div class="attach__delete"></div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment