๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

์ผ๋ฐ˜ <select multiple> ํƒœ๊ทธ๋Š” ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋””์ž์ธ์ด ๋‹ฌ๋ผ์„œ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์ด ์–ด๋ ต๋‹ค.
์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์ปค์Šคํ…€ ๋“œ๋กญ๋‹ค์šด ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฒดํฌ๋ฐ•์Šค๋กœ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ ,
์„ ํƒํ•œ ๊ฐ’์ด ์ƒ๋‹จ์— ํ‘œ์‹œ๋˜๋ฉฐ, ๊ธธ์–ด์ง€๋ฉด ... ์œผ๋กœ ์ž˜๋ฆฌ๊ณ  hover ์‹œ ํˆดํŒ์œผ๋กœ ์ „์ฒด ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ๊นŒ์ง€ ๊ตฌํ˜„ํ•œ๋‹ค.

 

See the Pen Untitled by note (@somieee) on CodePen.

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๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ์‹๋„ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ ํ•œ๋ฒˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธธ ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹๋‹ค.

 
๋ฐ˜์‘ํ˜•