Zoeeey 2023. 1. 31. 15:44

4. ๋ฐ”์ง€ ์„ ํƒํ•˜๋ฉด array์— ์ €์žฅํ•œ ๋ฐ”์ง€์‚ฌ์ด์ฆˆ ์…€๋ ‰๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ

์กฐ๊ฑด

๋ฐ”์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด array์— ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ์ด์ฆˆ ์…€๋ ‰๋ฐ•์Šค์— ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค. 


ํ‹€๋ฆฐ ํ’€์ด

<form class="container my-5 form-group">
  <p>์ƒํ’ˆ ์„ ํƒ</p>
  <select class="form-select mt-2">
    <option>๋ชจ์ž</option>
    <option>์…”์ธ </option>
    <option>๋ฐ”์ง€</option>
  </select>
  <select class="form-select form-popup mt-2 form-hide">
    <option>100</option>
  </select>
</form>
let pants = [28, 30, 32];

let selectBox = document.querySelector('.form-select');
let selectSize = document.querySelector('.form-popup');

function pantsSize() {
    if (selectBox.value == '๋ฐ”์ง€') {
        selectSize.classList.remove('form-hide');

        for (i = 0; selectSize.length = pants.length; i++) {
            document.querySelectorAll('.form-popup option')[i].innerHTML = pants[i];
            selectSize.appendChild(document.createElement('option'));
        }
    } else { selectSize.classList.add('form-hide'); }
}
selectBox.addEventListener('input', pantsSize);

์ˆ˜์น˜์‚ฌํ• ๊นŒ ๋ด ์˜ฌ๋ฆด๊นŒ ๋ง๊นŒ ๊ณ ๋ฏผ ๋งŽ์ด ํ–ˆ๋Š”๋ฐ.. for๋ฌธ ์กฐ๊ฑด์‹์„ ๋ถ€๋“ฑํ˜ธ๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋นˆ ์…€๋ ‰๋ฐ•์Šค๋กœ ๋…ธ์ถœ๋˜์–ด ๋“ฑํ˜ธ๋กœ ๋ฐ”๊ฟจ๋”๋‹ˆ ์ž‘๋™์ด ๋˜์–ด๋ฒ„๋ ค์„œ ๋“ฑํ˜ธ๋กœ ํ–ˆ๋‹ค. ์ด๋Š” ๋‹น์—ฐํ•œ ๊ฑด๋ฐ, ๋“ฑํ˜ธ๊ฐ€ ์„ ์–ธ์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜์—ฌ ๋ฌดํ•œ๋ฃจํ”„ ์กฐ๊ฑด์‹์ด ๋˜์–ด๋ฒ„๋ฆฐ ๊ฒƒ.


์ž‘๋™ํ•˜๋Š” ํ’€์ด

let pants = [28, 30, 32];

let selectBox = document.querySelector('.form-select');
let selectSize = document.querySelector('.form-popup');

function pantsSize() {
    if (selectBox.value == '๋ฐ”์ง€') {
        selectSize.classList.remove('form-hide');

        for (i = selectSize.length; i < pants.length; i++) {
            selectSize.appendChild(document.createElement('option'));
            document.querySelectorAll('.form-popup option')[i].innerHTML = pants[i];
        }
    } else { selectSize.classList.add('form-hide'); }
}
selectBox.addEventListener('input', pantsSize);

์˜๋ฌธ์ด์—ˆ๋˜ ๋ถ€๋ถ„

์™œ์ธ์ง€ let selectSizeElement = document.querySelectorAll('.form-popup option'); ์œผ๋กœ option์„ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด ๋‘๊ณ  for๋ฌธ์•ˆ์—  document.querySelectorAll('.form-popup option')[i]์„ selectSizeElement[i]์œผ๋กœ ์ฃผ๋‹ˆ selectSizeElement[i]๊ฐ€ undefined๊ฐ€ ๋‚˜์™”๋‹ค. ๋””๋ฒ„๊น… ์ง์ ‘ document.querySelectorAll('.form-popup option')[i]์œผ๋กœ ์ฝ˜์†”์— ์ฐ์–ด๋ณด๋ฉด ๊ฐ’์ด ์ œ๋Œ€๋กœ ๋‚˜์™”๋‹ค. ๋ณ€์ˆ˜ ์ˆ˜์— ํ• ๋‹นํ•œ ๊ฐ’์„ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์ฃผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š”๊ฑธ๊นŒ?

์˜๋ฌธ ํ•ด๊ฒฐ

document.querySelectorAll('.form-popup option')์„ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•˜๋‹ˆ(if๋ฌธ ์ฒซ์ค„์— ๋„ฃ์–ด๋„ ๋˜‘๊ฐ™๋‹ค) createElement๋กœ ์ƒˆ๋กœ์šด option์„ ๋งŒ๋“ค๊ธฐ ์ „์— ์ฝ์–ด๋ฒ„๋ ค์„œ ์ธ๋ฑ์Šค๋“ค์ด ์ฝ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

for (i = selectSize.length; i < pants.length; i++) {
    selectSize.appendChild(document.createElement('option'));
    let selectSizeElement = document.querySelectorAll('.form-popup option'); //์—ฌ๊ธฐ
    selectSizeElement[i].innerHTML = pants[i];
}

(๊ทธ๋ž˜์„œ ์ด๊ฑด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค)


forEach ๋ฒ„์ „

let pants = [28, 30, 32];

let selectBox = document.querySelector('.form-select');
let selectSize = document.querySelector('.form-popup');

function selectSizePop() {
    if (selectBox.value == '๋ฐ”์ง€') {
        selectSize.classList.remove('form-hide');
        selectSize.innerHTML = '';
        
        pants.forEach(function(pantsSize){
            selectSize.insertAdjacentHTML('beforeend', `<option>${pantsSize}</option>`);
        });
    } else { selectSize.classList.add('form-hide'); }
}
selectBox.addEventListener('input', selectSizePop);

์ถœ์ฒ˜ : ์ฝ”๋”ฉ์• ํ”Œ https://codingapple.com/