다중 셀렉트는 자주 사용되는 것이다.
간단히 동작 방법을 알아보자. 아래 방법을 가지고 응용하면 된다.

첫번째.
배열을 읽어와 보여 주는 방법이다. 가장 많이 사용하는 방법으로 데이타가 고정 적일때 사용한다.
배열을 따로 JS 파일을 만들어 읽어 오는 것이 좋다.
만일 배열 데이타가 수시로 바뀐다면 매번 JS 파일을 만들어 주어야 하기 때문에 여간 불편 한것이 아니다.
<form name=pubform method=post action=''>
<select name=selectName onChange="selectSecond(this.selectedIndex-1);">
<option value=''>1차 고르시오</option>
<option value='1'>1</option>
<option value='2'>2</option>
</select>
<select name=secondOption>
<option value=''>2차</option>
</select>
</form>

<script language="javascript">
function selectSecond(n) {
fs = document.pubform.secondOption;
if(n==-1) {
fs.selectedIndex = 0;
return;
} else {
for(j=0;j<arrSecondText[n].length;j++) {
fs.options[j]=new Option(arrSecondText[n][j],arrSecondValue[n][j]);
}
fs.length=arrSecondText[n].length;
}
}

arrSecondText = new Array(2);
arrSecondValue = new Array(2);

arrSecondText[0] = new Array(6);
arrSecondValue[0] = new Array(6);

arrSecondText[0][0] = "선택";
arrSecondValue[0][0] = "";
arrSecondText[0][1] = "옵션11";
arrSecondValue[0][1] = "11";
arrSecondText[0][2] = "옵션12";
arrSecondValue[0][2] = "12";
arrSecondText[0][3] = "옵션13";
arrSecondValue[0][3] = "13";
arrSecondText[0][4] = "옵션14";
arrSecondValue[0][4] = "14";
arrSecondText[0][5] = "옵션15";
arrSecondValue[0][5] = "15";

arrSecondText[1] = new Array(6);
arrSecondValue[1] = new Array(6);

arrSecondText[1][0] = "선택";
arrSecondValue[1][0] = "";
arrSecondText[1][1] = "옵션21";
arrSecondValue[1][1] = "21";
arrSecondText[1][2] = "옵션22";
arrSecondValue[1][2] = "22";
arrSecondText[1][3] = "옵션23";
arrSecondValue[1][3] = "13";
arrSecondText[1][4] = "옵션24";
arrSecondValue[1][4] = "24";
arrSecondText[1][5] = "옵션25";
arrSecondValue[1][5] = "25";
</script>

두번째.

아래 방법도 유용하다. 서버 스크립트로 요청된 셀렉트 값을 즉시 만들어 주는 방법이다.

위 방법보다는 느리지만 자주 바뀌는 데이타라면 아래 방법이 유용하다.

<form name=pubform method=post action=''>
<select name=selectName onChange="selectSecond();">
<option value=''>1차 고르시오</option>
<option value='1'>1</option>
</select>
<select name=secondOption>
<option value=''>2차</option>
</select>
</form>

<script id="selectOption"></script>
<script language="javascript">
function selectSecond() {
selectOption.src = "optionSelect.php";
}
</script>
2011/12/06 11:40 2011/12/06 11:40

Trackback Address :: https://youngsam.net/trackback/1713