Программисты Html
Anonymous
Как отключить невыполнение ранее выбранных параметров для множественного элемента выбора?
Сообщение
Anonymous » 23 фев 2025, 22:11
Я работаю над «Селектором тегов» ниже в JavaScript, без использования какого -либо плагина. < /p>
Код: Выделить всё
const tagsList = document.querySelector(".tags-list")
const tagActions = document.getElementById("tagActions")
const tagSelector = document.getElementById("tags")
const tagToggler = document.getElementById("tagSelectorToggler")
var selectedTags = new Set()
function filterTags(event) {
var query = event.target.value
var availableTags = Array.from(tagSelector.options)
availableTags.forEach(function (option) {
if (!option.text.toLowerCase().includes(query.toLowerCase())) {
option.classList.add("d-none")
} else {
option.classList.remove("d-none")
}
})
}
function toggleTagSelector(event) {
if (event.target.tagName !== "BUTTON" && event.target.tagName !== "SPAN") {
tagActions.classList.toggle("d-none")
tagToggler.classList.toggle("active")
}
}
function renderTags() {
tagsList.innerHTML =
[...selectedTags]
.sort()
.map(
(tag) =>
`[*] >${tag}
×
`,
)
.join("") ||
`[*]Click and select one or more tags from the list`
for (const option of tagSelector.options) {
option.selected = selectedTags.has(option.textContent)
}
}
tagsList.addEventListener("click", function (event) {
if (event.target.tagName !== "BUTTON") return
const span = event.target.closest("LI").children[0]
selectedTags.delete(span.textContent)
renderTags()
})
tagSelector.addEventListener("change", function () {
selectedTags = new Set(
Array.from(tagSelector.options)
.filter((option) => option.selected)
.map((option) => option.textContent),
)
renderTags()
})< /code>
.tags-list {
min-height: 40px;
list-style-type: none;
}
.tags-list .tag {
line-height: 1;
white-space: nowrap;
background: #f2f2f2;
border: 1px solid #e6e3e3;
display: inline-flex;
align-items: center;
border-radius: 999px;
font-size: 13px;
padding: 3px 8px;
margin-bottom: 2px;
}
.tags-list .tag button {
background: #ff9292;
color: #720000;
border: none;
width: 18px;
height: 18px;
font-size: 12px;
display: inline-flex;
justify-content: center;
align-items: center;
margin-left: 6px;
border-radius: 50%;
}
.form-control:focus, .form-select:focus {
box-shadow: none;
}
/* Tags */
.toggler {
position: absolute;
right: 5px;
margin-top: 8px;
color: #777;
cursor: pointer;
}
.toggler.active {
transform: rotate(180deg);
}
.tag-select option {
padding: 3px 8px;
}< /code>
[*]
Tags
[i][/i]
[list] class="form-control tags-list mb-1"
onclick="toggleTagSelector(event)"
>
Click and select one or more tags from the list
[/list]
Apple
Banana
Blackberry
Blueberry
Cherry
Cranberry
Grapes
Kiwi
Mango
Orange
Peach
Pear
Pineapple
Raspberry
Strawberry
Watermelon
< /code>
< /div>
< /div>
< /p>
Я разработал способ поиска (фильтр) через список доступных Теги, если это элемент
. ранее выбранные элементы Открытый.>
Подробнее здесь:
https://stackoverflow.com/questions/794 ... select-ele
1740337888
Anonymous
Я работаю над «Селектором тегов» ниже в JavaScript, без использования какого -либо плагина. < /p> [code]const tagsList = document.querySelector(".tags-list") const tagActions = document.getElementById("tagActions") const tagSelector = document.getElementById("tags") const tagToggler = document.getElementById("tagSelectorToggler") var selectedTags = new Set() function filterTags(event) { var query = event.target.value var availableTags = Array.from(tagSelector.options) availableTags.forEach(function (option) { if (!option.text.toLowerCase().includes(query.toLowerCase())) { option.classList.add("d-none") } else { option.classList.remove("d-none") } }) } function toggleTagSelector(event) { if (event.target.tagName !== "BUTTON" && event.target.tagName !== "SPAN") { tagActions.classList.toggle("d-none") tagToggler.classList.toggle("active") } } function renderTags() { tagsList.innerHTML = [...selectedTags] .sort() .map( (tag) => `[*] >${tag} × `, ) .join("") || `[*]Click and select one or more tags from the list` for (const option of tagSelector.options) { option.selected = selectedTags.has(option.textContent) } } tagsList.addEventListener("click", function (event) { if (event.target.tagName !== "BUTTON") return const span = event.target.closest("LI").children[0] selectedTags.delete(span.textContent) renderTags() }) tagSelector.addEventListener("change", function () { selectedTags = new Set( Array.from(tagSelector.options) .filter((option) => option.selected) .map((option) => option.textContent), ) renderTags() })< /code> .tags-list { min-height: 40px; list-style-type: none; } .tags-list .tag { line-height: 1; white-space: nowrap; background: #f2f2f2; border: 1px solid #e6e3e3; display: inline-flex; align-items: center; border-radius: 999px; font-size: 13px; padding: 3px 8px; margin-bottom: 2px; } .tags-list .tag button { background: #ff9292; color: #720000; border: none; width: 18px; height: 18px; font-size: 12px; display: inline-flex; justify-content: center; align-items: center; margin-left: 6px; border-radius: 50%; } .form-control:focus, .form-select:focus { box-shadow: none; } /* Tags */ .toggler { position: absolute; right: 5px; margin-top: 8px; color: #777; cursor: pointer; } .toggler.active { transform: rotate(180deg); } .tag-select option { padding: 3px 8px; }< /code> [*] Tags [i][/i] [list] class="form-control tags-list mb-1" onclick="toggleTagSelector(event)" > Click and select one or more tags from the list [/list] Apple Banana Blackberry Blueberry Cherry Cranberry Grapes Kiwi Mango Orange Peach Pear Pineapple Raspberry Strawberry Watermelon < /code> < /div> < /div> < /p> Я разработал способ поиска (фильтр) через список доступных Теги, если это элемент [/code]. ранее выбранные элементы Открытый.> Подробнее здесь: [url]https://stackoverflow.com/questions/79461816/how-to-disable-unselecting-previously-selected-options-for-a-multiple-select-ele[/url]
0 Ответы
16 Просмотры
Последнее сообщение Anonymous
23 фев 2025, 22:11
0 Ответы
11 Просмотры
Последнее сообщение Anonymous
23 фев 2025, 22:11
0 Ответы
12 Просмотры
Последнее сообщение Anonymous
23 фев 2025, 23:32
0 Ответы
15 Просмотры
Последнее сообщение Anonymous
24 фев 2025, 06:48
0 Ответы
16 Просмотры
Последнее сообщение Anonymous
24 фев 2025, 06:48