๊ฐ์ธ ํ๋ก์ ํธ ํ์๋ฑ๋ก ํ์ด์ง ํ๋ก ํธ ์์ ์ ํ๋๋ฐ
select ํ๊ทธ option ํ๊ทธ ๋์์ธ์ด ๋ง์์ ๋ค์ง ์์ ์ปค์คํ ๋ฐฉ๋ฒ์ ์ฐพ์์ผ๋..
ํ๋ฌ๊ทธ์ธ์ผ๋ก ์ ์ดํ๋ ๋ฐฉ๋ฒ๋ฐ์๋ ์๋ค๋ ์ฌ์ค์ ์๊ฒ ๋์๋ค.
https://jqueryniceselect.hernansartorio.com/
jQuery Nice Select
Usage 1. Include jQuery and the plugin. 2. Include the plugin styles, either the compiled CSS... ...or, ideally, import the SASS source (if you use SASS) in your main stylesheet for easier customization. @import 'nice-select'; // Or 'nice-select-prefixed'.
jqueryniceselect.hernansartorio.com
๊ทธ๋ฐ๋ฐ ์ฐพ์ ํ๋ฌ๊ทธ์ธ์ด ๋์ค์ ์ผ๋ก ์ด์ฉํ๋ ๊ฒ ๊ฐ์ง๋ ์๊ณ
์ด ์ฝํ ์ธ ํ๋ ๋๋ฌธ์ ์๋ก์ด ํ๋ฌ๊ทธ์ธ์ ์ฐ๊ฒฐํ๊ณ ์ฐ๋ ๊ฒ์ ์ํ์ง ์์๋ค.
js๋ jQuery๋ง์ผ๋ก ํด๋น ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์์๊น ์ฐพ์๋ณด๋ ์ค,
๋ค์ด๋ฒ ํ์๊ฐ์ ์ฐฝ์ ๋ด๊ฐ ๊ตฌํํ๊ณ ์ํ๋ ๊ธฐ๋ฅ์ด ๋์ผํ๊ฒ ์กด์ฌํ๊ณ
select ํ๊ทธ๊ฐ ์๋ divํ๊ทธ, li ๊ตฌ์กฐ ๋ด์ button ํ๊ทธ๋ก ๊ตฌํ๋์ด ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค!!


๋์ค์ button ํ๊ทธ ์์ ๊ฐ์ ์ด๋ค ์์ผ๋ก java๋ก ๊ฐ์ ธ๊ฐ์ง๋ ์ ์ ์์์ง๋ง
์ด ๊ตฌ์กฐ๋ก ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์์์ผ๋ ๋์์ธ ์ ํ๊ณ ๋์ค์ ์ด๋ป๊ฒ๋ ๋๊ฒ ์ง ~ ํ๋ ์๊ฐ์ผ๋ก
ํด๋น ์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํด ๋น์ทํ ๊ตฌ์กฐ๋ก ๋์์ธ์ ๊ตฌํํ๋ค ... ๐

<section class="input_area">
<div>
<!-- checked ๋์๋ option ํ๊ทธ ์ญํ ์ ํ button ํ๊ทธ -->
<button type="button" aria-expanded="false" class="toggle_btn">
ํต์ ์ฌ
</button>
<!-- ํ์ option ํ๊ทธ ์์ญ -->
<ul class="telecom_area">
<li>
<button type="button" name="userTelecom" class="option_btn" value="kt">KT</button>
</li>
<li>
<button type="button" name="userTelecom" class="option_btn" value="skt">SKT</button>
</li>
<li>
<button type="button" name="userTelecom" class="option_btn" value="lg">LG</button>
</li>
</ul>
</div>
<!-- ์ ํ๋ฒํธ ์
๋ ฅ ์์ญ -->
<input type="text" placeholder="์ ํ๋ฒํธ"
onfocus="this.placeholder=''"
onblur="this.placeholder='์ ํ๋ฒํธ'"
oninput="hypenTel(this)" maxlength="13"
name="userTel">
</section>
html ๊ณผ css๋ก ๋์์ธ ๊ตฌํ์ ์ฌ์ ๋ค! ๊ทธ๋ฐ๋ฐ select ํ๊ทธ์ option ํ๊ทธ๋ก ๊ตฌํํ ๋ ์๋์ผ๋ก ์ ๊ณตํด์ฃผ๋ ํ ๊ธ ๊ธฐ๋ฅ์ด ์ด๋ฐ์์ผ๋ก ๋ง๋ค๊ฒ ๋๋ฉด ์ ๊ณต๋์ง ์๋๋ค..
select ํ๊ทธ์ฒ๋ผ select ์์ญ์ ํด๋ฆญํ๋ฉด ํ์์ option ๋ฉ๋ด๊ฐ ๋ํ๋์ผ ํ๊ณ
ํ์ option ๋ฉ๋ด๋ฅผ ํด๋ฆญํ๋ฉด ํต์ ์ฌ๋ผ๊ณ ์ ํ์ ธ ์๋ ๋ถ๋ถ์ด ํด๋ฆญํ ๋ฉ๋ด์ ๋ด์ฉ์ผ๋ก ๋ฐ๋์ด์ผ ํ๋ค.
๊ฐ์ฅ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ฐพ๋ค๊ฐ
aria-expended ๋ผ๋ ์์ฑ์ ์๊ฒ ๋์ด, aria-expended ์์ฑ์ ์ด์ฉํด ํต์ ์ฌ ๋ฒํผ์ ํด๋ฆญํ์ ๋ kt, skt, lg ํ์ ๋ฉ๋ด๊ฐ ๋ณด์ด๋๋ก jQuery๋ฅผ ์ด์ฉํด ๊ตฌํ ์๋ฃํ๋ค. ( ์ฐธ๊ณ ์ฌ์ดํธ์๋ ๋ค์ด๋ฒ๋ ์ด ์์ฑ์ ์ด์ฉํด ๊ตฌํ๋์ด ์์๋ค.)
<button type="button" aria-expanded="false" class="toggle_btn">
ํต์ ์ฌ
</button>
// TELECOM ์์ญ ํ ๊ธ
$(".toggle_btn").click(function(e) {
$(this).toggleClass("on");
e.stopPropagation();
if ($(this).attr("aria-expanded")) {
$(this).attr("aria-expanded", "false");
$(this).siblings('.telecom_area').css({'display':'none'});
} else {
$(this).attr("aria-expanded", "true");
$(this).siblings('.telecom_area').css({'display':'flex'});
}
})
ํต์ ์ฌ ๋ฒํผ ์์์ aria-expanded ์์ฑ์ ์ถ๊ฐํ๊ณ ๊ธฐ๋ณธ value๋ฅผ false๋ก ์ค์ ํ ํ if๋ฌธ์ ์ด์ฉํด ํด๋ฆญํ์ ๋ ๊ฐ์ true๋ก ๋ณ๊ฒฝํ๋ฉฐ ์ต์ ์์ญ์ css๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋ ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ค.
๊ทธ๋ฐ๋ฐ ์ฌ๊ธฐ์์ ํ๊ฐ์ง ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
๊ธฐ๋ณธ value๋ฅผ false๋ก ์ค์ ํ์ง๋ง $(this).attr("aria-expanded")๋ ๊ณ์ true๋ก ๋์ค๊ณ ์์๋ค.
attr ๋ฉ์๋๋ฅผ ์ด์ฉํด aria-expanded์ ์์ฑ๊ฐ์ ๋ฐํํ์ผ๋ attr ๋ฉ์๋ ์์ฒด๊ฐ value๋ฅผ ๋ฌธ์ํํ๋ก ๋ณํํด ๋ฐํํ๊ธฐ ๋๋ฌธ์ false๊ฐ boolean ํ์ด ์๋ ๋ฌธ์ 'false'๋ก ๋ฐํ๋์๊ณ ๊ทธ๋์ ๊ณ์ ์ถ๋ ฅํ์ ๋ true๋ก ๋์ค๊ณ ์์๋ ๊ฒ!
๋ฐํ ๋ฐ์ดํฐ๊ฐ ๋ฌธ์์ด true์ธ์ง ๊ฒ์ฆํ๋ฉด ๋๊ฒ ๋ค๋ผ ์๊ฐํด์
$(this).attr("aria-expanded") === "true" ๋ก ์กฐ๊ฑด์์ ๋ฐ๊ฟ ํด๊ฒฐํ๊ฒ ๋์๋ค ๐
// .toggle_btn(ํต์ ์ฌ ๋ฒํผ) ํด๋ฆญ์ ์คํ
$(".toggle_btn").click(function(e) {
// ํด๋ฆญํ ํต์ ์ฌ ๋ฒํผ์ on ํด๋์ค๊ฐ ์๋ค๋ฉด ์ ๊ฑฐ, ์๋ค๋ฉด ์ถ๊ฐ (ํ์ดํ ์ ์ด์ฉ ์๋ณ์)
$(this).toggleClass("on");
// ํ์ ๋ฉ๋ด๋ฅผ ํด๋ฆญํ์ ๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ ์คํ ๋ฉ์ถค
e.stopPropagation();
// ํด๋ฆญํ ๋ฒํผ ์์์ aria-expanded ์์ฑ ๊ฐ ๋น๊ต
if ($(this).attr("aria-expanded") === "true") {
$(this).attr("aria-expanded", "false");
$(this).siblings('.telecom_area').css({'display':'none'});
} else {
$(this).attr("aria-expanded", "true");
$(this).siblings('.telecom_area').css({'display':'flex'});
}
})

์ฌ๊ธฐ์์ ํ์์ ๋ฉ๋ด๋ฅผ ํด๋ฆญํ๋ฉด ํต์ ์ฌ๋ผ๋ ํ ์คํธ ์์๊ฐ ํด๋ฆญํ ๋ฉ๋ด์ ๋ด์ฉ์ผ๋ก ๊ต์ฒด๋์ด์ผ ํ๋ค.
์๋ฅผ ๋ค๋ฉด ํ์ ๋ฉ๋ด KT๋ฅผ ํด๋ฆญํ๋ฉด ํ์ ๋ฉ๋ด ์์ญ์ด ์ฌ๋ผ์ง๊ณ ํต์ ์ฌ๋ผ๊ณ ์ ํ์ ธ ์๋ ๋ถ๋ถ์ด ๋ด๊ฐ ํด๋ฆญํ KT๋ก ๋ด์ฉ์ด ์์ ๋์ด์ผ ํ๋ค. ์ด ๊ธฐ๋ฅ๋ select์ option ํ๊ทธ๋ฅผ ์ด์ฉํ๋ค๋ฉด ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณต๋ ๊ธฐ๋ฅ์ด์์ผ๋...
button ํ๊ทธ ๊ตฌ์กฐ๋ก ๊ตฌํํ๊ธฐ ๋๋ฌธ์ jQuery๋ก ๋ฐ๋ก ๊ตฌํํด์ผ ํ๋ค. ๐
// ํ์ ์ต์
๋ฉ๋ด ํด๋ฆญ์ ์คํ
$(".option_btn").on("click",function(){
// ๋ณ์ btn_text๋ฅผ ์ ์ธํ๊ณ ํด๋ฆญํ ๋ฉ๋ด์ text ์์๋ฅผ ๋์
let btn_text = $(this).text();
// ํต์ ์ฌ ๋ฉ๋ด์ ํ
์คํธ ๋ด์ฉ์ ๋ณ์ btn_text๋ก ๋์ฒด
$('.toggle_btn').text(btn_text);
// ๋ฉ๋ด ๋ด์ฉ์ด ๋ฐ๋๊ณ ํ์ ๋ฉ๋ด๋ฅผ ๋ค์ close ํ๊ธฐ ์ํ ์ฝ๋
$(".toggle_btn").attr("aria-expanded", "false");
$('.toggle_btn').removeClass('on');
$(".toggle_btn").siblings('.telecom_area').css({'display':'none'});
})

์ด๋ ๊ฒ ํ๋ก ํธ ์์ ์ ๋ง๋ฌด๋ฆฌํ๊ณ jsp๋ก ์ ๋ ฅ ๊ฐ์ ๊ฐ์ ธ์ค๋ ์์ ์ ์งํํ๋ค.
input ํ๊ทธ๋ select ํ๊ทธ๋ฅผ ์ด์ฉํ ์ผ๋ฐ์ ์ธ ์์ ๋ฐฉ๋ฒ์ด์๋ค๋ฉด submit ๋ฒํผ์ ํด๋ฆญํ์ ๋ ์๋ฒ๋ก ์ ์ถ๋์ดrequest.getParameter ๋ฉ์๋์ name ์์ฑ ๊ฐ์ ์ธ์๋ก ๋ฃ์ด์ฃผ๋ฉด ๊ฐ๋จํ ๊ฐ์ ธ์ฌ ์ ์์์ง๋ง, button ์์๋ submit์ผ๋ก ๊ฐ์ด ์๋์ผ๋ก ์ ์ก๋์ง ์๊ธฐ ๋๋ฌธ์ ์ถ๊ฐ์ ์ธ ์์ ์ ์งํํด์ค์ผ ํ๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ์ hidden ํ์ ์ input ํ๊ทธ๋ฅผ ํ๋ ๋ฃ๊ณ
๋ฒํผ ์์๋ฅผ ํด๋ฆญํ ๋๋ง๋ค ํด๋ฆญํ ๋ฒํผ ์์์ value ๊ฐ์ input์ value ๊ฐ์ผ๋ก ๋์ ํด submitํด์ฃผ๋ ๋ฐฉ๋ฒ์ด์๋ค.
<input type="hidden" class="user_telecom_input" name="userTelecom">
// ํต์ ์ฌ button value ๊ฐ input ์ฝ๋
$(".option_btn").on("click",function(){
// ๋ณ์ value๋ฅผ ์ ์ธํ๊ณ ํด๋ฆญํ ๋ฒํผ์ value ๊ฐ์ ๋์
var value = $(this).val();
// hidden ํ์
์ผ๋ก ๋ฃ์ input ์์์ value ๊ฐ์ผ๋ก ๋ณ์ value ๋์
$(".user_telecom_input").val(value);
})
ํด๋ฆญํ ๋ฉ๋ด์ value๊ฐ hidden์ผ๋ก ์จ๊ฒจ๋ input์ผ๋ก ๋ค์ด๊ฐ๊ณ
submit ํ ๋์๋ hidden ํ์ ์ input์ value๊ฐ์ด ์ ์ก๋ ๊ฒ์ด๋ค!
java์์๋ hidden input์ name ๊ฐ์ผ๋ก ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ ธ์ค๋ฉด ๋๋ค.
String userTelecom = request.getParameter("userTelecom"); // ํต์ ์ฌ
