๋ฐ์ํ
์ผ๋ฐ <select multiple> ํ๊ทธ๋ ๋ธ๋ผ์ฐ์ ๋ง๋ค ๋์์ธ์ด ๋ฌ๋ผ์ ์ปค์คํฐ๋ง์ด์ง์ด ์ด๋ ต๋ค.
์ด๋ฒ ๊ธ์์๋ ์ปค์คํ
๋๋กญ๋ค์ด ๋ฐ์ค๋ฅผ ๋ง๋ค์ด์ ์ฒดํฌ๋ฐ์ค๋ก ์ฌ๋ฌ ๊ฐ๋ฅผ ์ ํํ ์ ์๊ณ ,
์ ํํ ๊ฐ์ด ์๋จ์ ํ์๋๋ฉฐ, ๊ธธ์ด์ง๋ฉด ... ์ผ๋ก ์๋ฆฌ๊ณ hover ์ ํดํ์ผ๋ก ์ ์ฒด ๊ฐ์ ๋ณด์ฌ์ฃผ๋ ๊ธฐ๋ฅ๊น์ง ๊ตฌํํ๋ค.
1.HTML ๊ตฌ์กฐ
<div class="custom-select" id="fruitSelect">
<span class="selected">๊ณผ์ผ ์ ํ</span>
<div class="options">
<label><input type="checkbox" class="fruit-checkbox" value="์ฌ๊ณผ"> ์ฌ๊ณผ</label>
<label><input type="checkbox" class="fruit-checkbox" value="๋ฐ๋๋"> ๋ฐ๋๋</label>
<label><input type="checkbox" class="fruit-checkbox" value="ํฌ๋"> ํฌ๋</label>
<label><input type="checkbox" class="fruit-checkbox" value="๋ธ๊ธฐ"> ๋ธ๊ธฐ</label>
<label><input type="checkbox" class="fruit-checkbox" value="์๋ฐ"> ์๋ฐ</label>
</div>
</div>
2.CSS ์คํ์ผ
.custom-select {
width: 200px;
border: 1px solid #ccc;
cursor: pointer;
position: relative;
}
.custom-select .selected {
display: block;
padding: 5px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* ๊ธธ๋ฉด ... */
}
.custom-select .options {
display: none;
position: absolute;
background: #fff;
border: 1px solid #ccc;
max-height: 150px;
overflow-y: auto;
width: 100%;
z-index: 1000;
}
.custom-select .options label {
display: block;
padding: 5px;
cursor: pointer;
}
3. jQuery ๊ธฐ๋ฅ
$(function () {
// ๋๋กญ๋ค์ด ์ด๊ธฐ/๋ซ๊ธฐ
$(document).on("click", ".custom-select", function (e) {
e.stopPropagation();
$(".custom-select .options").not($(this).find(".options")).hide();
$(this).find(".options").toggle();
});
// ์ฒดํฌ๋ฐ์ค ํด๋ฆญ ์ ๋ซํ์ง ์๋๋ก
$(document).on("click", "#fruitSelect .options input[type=checkbox], #fruitSelect .options label", function (e) {
e.stopPropagation();
});
// ๋ฐ๊นฅ ํด๋ฆญํ๋ฉด ๋ซ๊ธฐ
$(document).click(function () {
$(".custom-select .options").hide();
});
// ์ฒดํฌ๋ฐ์ค ์ ํ ์ ์ ํ๋ ๊ฐ ํ์ + title ํดํ
$(document).on("change", "#fruitSelect .fruit-checkbox", function () {
let selected = [];
$("#fruitSelect .fruit-checkbox:checked").each(function () {
selected.push($(this).val());
});
if (selected.length > 0) {
let joined = selected.join(", ");
$("#fruitSelect .selected")
.text(joined)
.attr("title", joined);
} else {
$("#fruitSelect .selected")
.text("๊ณผ์ผ ์ ํ")
.removeAttr("title");
}
});
});
๋์ ์์
- ์ฌ๋ฌ ๊ฐ ์ ํ →
์ฌ๊ณผ, ๋ฐ๋๋, ํฌ๋, ... - ๋ง์ฐ์ค hover →
ํดํ์ ์ฌ๊ณผ, ๋ฐ๋๋, ํฌ๋ ์ ์ฒด ํ์
์ด๋ ๊ฒ ํ๋ฉด Ajax๋ก ๋ฐ์ดํฐ๋ฅผ ๋์ ์ผ๋ก ๋ถ๋ฌ์ค๋ ๋ฐฉ์๋ ์ ์ฉ ๊ฐ๋ฅํ๋ฉฐ ํ๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ๋๊ธธ ๋ ์ฌ์ฉํ๊ธฐ ์ข๋ค.
๋ฐ์ํ
'๐ฅ๏ธ ํ๋ก ํธ์๋ > ๐จํผ๋ธ๋ฆฌ์ฑ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| # ๋ถํธ์คํธ๋ฉ ๋ฐ ๋ถํธ์คํธ๋ฉ ์์ด์ฝ(bootstrap icons) CDN (0) | 2025.09.15 |
|---|---|
| # ์ต์ ์ ์ด์ฟผ๋ฆฌ(jQuery), ์ ์ด์ฟผ๋ฆฌ UI CDN (0) | 2025.09.10 |
| # CSS - placeholder ์์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ (2) | 2025.09.01 |
| # ๊ฐ์ฅ ๊น๋ํ ํ๊ตญ์ด ์นํฐํธ Pretendard, ํ๋ก์ ํธ์ ์ ์ฉํ๊ธฐ(CDN) (4) | 2025.08.28 |
| # spring boot TailwindCSS์ ์ฐ๋ํ๊ธฐ, ์ฌ์ฉ๋ฐฉ๋ฒ (7) | 2025.07.07 |