๋ด๊ฐ (์ฒดํฌ)์ ํํ ์์๋๋ก ์ ๋ ฅ๋๋๋ก.
์ต๊ทผ์ ์ ํํ ๊ฒ์ด ๊ฐ์ฅ ๋์ค์.
์ฒดํฌ ํด์ ํ๋ฉด ๊ทธ ๋ด์ฉ๋ง ์ญ์
์ฒดํฌํ๋ฉด ์๋์ ์ฒดํฌํ ๋ด์ฉ ํ ์คํธ ์จ์ฃผ๊ธฐ
์๋์ผ๋ก ๊ฐ๋ณ์ฒดํฌํ์ ๋ ์ ์ ๋ ์๋์ผ๋ก ์ฒดํฌ๋๊ณ , ํ๋๋ง ์ฒดํฌ ํ์ด๋ ์ฒดํฌํด์ ๋๊ฒ
์ฒดํฌ ๊ฐ์๊ฐ 4์ดํ, 9์ผ ๋ ๋ฒํผ ๋๋ฅด๋ฉด ์ฑ๊ณต alert
๊ธฐ๋ฅ ๊ตฌํํ๋ฉฐ ์๋ก ๋ฐฐ์ด ๋ด์ฉ
- $(์ ํ์).arrt("์์ฑ ์ด๋ฆ","์์ฑ ๊ฐ")์ $(์ ํ์). removeAttr("์์ฑ ์ด๋ฆ")์ jQuery 1.6 ์ด์ ๋ฒ์ ์์๋ ๋์ํ์ง๋ง, ์ต์ ๋ฒ์ ์์๋ .prop() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผํจ
- ์์ฑ ์ถ๊ฐ ์์: $("#allChk").prop("checked", true);
- ์์ฑ ์ ๊ฑฐ ์์: $("#allChk").prop("checked", false);
- ์ ์ด์ฟผ๋ฆฌ์์ ํ๊ทธ๋ก ์ ํํด์ ๋ฐ๋ณตํ๊ธฐ + ์ฒซ๋ฒ์งธ๊ฐ ์ ์ธ + if ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด click์ด๋ฒคํธ ์ผ์ด๋๊ฒ ํ๊ธฐ
$("input[type='checkbox']").not(":first").each(function () {
if (!$(this).is(":checked")) {
$(this).trigger("click");
}
});
ํ๋ฉด ์์
JQuery
$(function () {
// let isAllChecked = false;
let checkedCount = 0;
//์ ์ฒด ์ฒดํฌ๋ฐ์ค๋ฅผ ์ ์ธํ๊ณ checkedํ์
์ input์ click ํจ์ ๋ฃ๊ธฐ(textarea๊ด๋ จ)
$("input[type='checkbox']").not(":first").on("click", function() {
ckeckedAndTextare($(this));
});
function ckeckedAndTextare (element){
if(element.is(":checked")) {
//์ฒดํฌ ๋์์ ๋ textArea์ ์ถ๊ฐ
$("#regionText").val($("#regionText").val()+element.val()+" ");
//์ฒดํฌ๊ฐ์ ์ถ๊ฐ
checkedCount = checkedCount+1;
//์ด๋ฒ ์ฒดํฌ๋ก ์ ์ฒด ์ฒดํฌ๊ฐ ๋๋๊ฑฐ๋ผ๋ฉด
if(checkedCount==9){
$("#allChk").prop("checked", true);
}
} else {
//์ฒดํฌ๋์ง ์์์ ๋ ๊ธ์ ์ญ์
let textAreaVale = $("#regionText").val();
//์ฒดํฌ ๊ฐ์
checkedCount = checkedCount-1;
$("#regionText").val(textAreaVale.replace(element.val()+" ",""));
// ์ด๋ฒ ์ฒดํฌ ํด์ ๋ก ์ ์ฒด๊ฐ ํ๋ฆฌ๊ฒ ๋๋๊ฑฐ๋ผ๋ฉด ์ ์ฒด ๋ฒํผ checked ์ง์์ฃผ๊ธฐ
if(checkedCount!=9){
$("#allChk").prop("checked", false);
}
}
}
// ์ ์ฒด ๋ฒํผ์ ๋๋ฅด๋ฉด ์ฒดํฌ๋์ง ์์ ํ๊ทธ๋ฅผ ํ์ธํ๊ณ click ํจ์๋ฅผ ์คํํ๋ค.
$("#allChk").on("click", function () {
if ($("#allChk").is(":checked")) {
// ์ ์ฒด checked์ด๋ฉด checked false์ธ ๊ฒ๋ค์ true๋ก
$("input[type='checkbox']").not(":first").each(function () {
if (!$(this).is(":checked")) {
$(this).trigger("click");
}
});
} else {
// ์ ์ฒด checked ์๋๋ฉด checked true์ธ ๊ฒ๋ค์ false๋ก
$("input[type='checkbox']").not(":first").each(function () {
if ($(this).is(":checked")) {
$(this).trigger("click");
}
});
}
});
//ํ์ธ ๋ฒํผ
$("#btn").click(function (){
if(checkedCount<4||checkedCount==9){
alert("[์ฑ๊ณต] ์ฒดํฌ ์นด์ดํธ : "+checkedCount);
} else {
alert("[์คํจ] ์ฒดํฌ ์นด์ดํธ : "+checkedCount);
}
});
});