자바스크립트 name값이 배열일때 접근
페이지 정보
작성자 서방님 댓글 0건 조회 315회 작성일 13-04-11 13:10본문
자바스크립트에서 폼의 객체 이름을 "objName[]" 식으로 지을 경우가 있다.
보통 php쪽에서 배열로 받기 위해서 위와 같이 짓는데...
이를 자바스크립트에서는 배열로 인식하지 못해 조금 난처한 경우가 많다.
보통 php쪽에서 배열로 받기 위해서 위와 같이 짓는데...
이를 자바스크립트에서는 배열로 인식하지 못해 조금 난처한 경우가 많다.
가령 다음과 같은 경우 오류가 난다
<HTML>
<HEAD>
</head>
<HEAD>
</head>
<script language="Javascript">
<!--
function objLength() {
alert!(document.testForm.checkObj[].length);
}
//-->
</script>
<!--
function objLength() {
alert!(document.testForm.checkObj[].length);
}
//-->
</script>
<form name="testForm">
<input type="checkbox" name="checkObj[]"> aaa
<input type="checkbox" name="checkObj[]"> bbb
<input type="checkbox" name="checkObj[]"> ccc
<input type="checkbox" name="checkObj[]"> aaa
<input type="checkbox" name="checkObj[]"> bbb
<input type="checkbox" name="checkObj[]"> ccc
<a href="javascript:objLength();">체크박스의 개수는?</a>
</form>
</form>
이럴 경우에는 다음과 같이 처리해 주면 된다.
<script language="Javascript">
<!--
function objLength2() {
alert!(document.forms['testForm2']['checkObj[]'].length);
}
//-->
</script>
<!--
function objLength2() {
alert!(document.forms['testForm2']['checkObj[]'].length);
}
//-->
</script>
<form name="testForm2">
<input type="checkbox" name="checkObj[]"> aaa
<input type="checkbox" name="checkObj[]"> bbb
<input type="checkbox" name="checkObj[]"> ccc
<input type="checkbox" name="checkObj[]"> aaa
<input type="checkbox" name="checkObj[]"> bbb
<input type="checkbox" name="checkObj[]"> ccc
<a href="javascript:objLength2();">체크박스의 개수는?</a>
</form>
</form>
또한 각 개체로의 접근은 다음과 같이 사용하면 된다...
ex) document.forms['testForm2']['checkObj[]'][0].value
function search_chk(){
var tema_length = document.forms['form1']['P_Tema[]'].length
//alert(document.forms['form1']['P_Tema[]'][0].checked)
for(i=0; i < tema_length; i++){
if(document.forms['form1']['P_Tema[]'][i].checked==true) return true
}
alert('1개 이상 선택하세요')
return false
}
var tema_length = document.forms['form1']['P_Tema[]'].length
//alert(document.forms['form1']['P_Tema[]'][0].checked)
for(i=0; i < tema_length; i++){
if(document.forms['form1']['P_Tema[]'][i].checked==true) return true
}
alert('1개 이상 선택하세요')
return false
}
댓글목록
등록된 댓글이 없습니다.