f

출력 결과가 이상하게 다르네요....

안녕하세요.
자바스크립트 초보라 이렇게 질문 드립니다.
 
동일한 내용을 출력하는데 함수에서 호출 할 경우와 그렇지 않을 경우의 결과가 다릅니다.
왜 그런가요?
 
 
[예제1]
<html>
<head>
<script type="text/javascript">
function print_form()
{
 document.write(document.forms.length);
 document.write(document.forms[0].elements[1].name);
}
</script>
</head>
<body>
<form>
name:<input type="text" name="name" /><br />
phone:<input type="text" name="phone" /><br />
</form>
<form>
name2:<input type="text" name="name2" /><br />
phone2:<input type="text" name="phone2" /><br />
</form>
<input type="submit" onclick="print_form()" />

</body>
</html>
 
결과 : 2, name 이 정상 출력 되어야 하는데 이상하게 2만 출력이 되네요.
 
[예제2]
<html>
<head>
</head>
<body>
<form>
name:<input type="text" name="name" /><br />
phone:<input type="text" name="phone" /><br />
</form>
<form>
name2:<input type="text" name="name2" /><br />
phone2:<input type="text" name="phone2" /><br />
</form>
<script type="text/javascript">
document.write(document.forms.length);
document.write(document.forms[0].elements[0].name);
</script>

</body>
</html>
 
결과 : 2, name 이 정상 출력 됩니다.
|

댓글 3개

document.write(document.forms.length+document.forms[0].elements[1].name); 요렇게 처리해야 할듯 싶내요...
새로운 페이지가 생성되면서 잃으면서 다음건 출력 안되는거내요.
도통 모르겠네요
일단은 두 페이지가 소스가 다르네요

document.forms[0].elements[0].name
document.forms[0].elements[1].name

http://math88.com.ne.kr/computer/js/JSmanual/7-2-6-2.html
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 787
13년 전 조회 1,145
13년 전 조회 969
13년 전 조회 1,148
13년 전 조회 1,422
13년 전 조회 1,011
13년 전 조회 1,290
13년 전 조회 854
13년 전 조회 891
13년 전 조회 1,406
13년 전 조회 3,286
13년 전 조회 829
13년 전 조회 2,267
13년 전 조회 1,040
13년 전 조회 938
13년 전 조회 1,841
13년 전 조회 1,770
13년 전 조회 780
13년 전 조회 1,687
13년 전 조회 2,096
13년 전 조회 1,517
13년 전 조회 772
13년 전 조회 2,185
13년 전 조회 987
13년 전 조회 763
13년 전 조회 727
13년 전 조회 1,456
13년 전 조회 1,839
13년 전 조회 1,309
13년 전 조회 789
13년 전 조회 930
13년 전 조회 1,428
13년 전 조회 1,369
13년 전 조회 717
13년 전 조회 3,505
13년 전 조회 1,365
13년 전 조회 4,536
13년 전 조회 945
13년 전 조회 1,940
13년 전 조회 1,009
13년 전 조회 918
13년 전 조회 1,830
13년 전 조회 810
13년 전 조회 1,760
13년 전 조회 865
13년 전 조회 1,637
13년 전 조회 1,161
13년 전 조회 4,220
13년 전 조회 2,223
13년 전 조회 1,044
13년 전 조회 867
13년 전 조회 1,444
13년 전 조회 1,068
13년 전 조회 1,194
13년 전 조회 2,289
13년 전 조회 937
13년 전 조회 3,000
13년 전 조회 2,974
13년 전 조회 1,259
13년 전 조회 954
13년 전 조회 2,474
13년 전 조회 1,071
13년 전 조회 1,327
13년 전 조회 849
13년 전 조회 1,968
13년 전 조회 1,279
13년 전 조회 1,336
13년 전 조회 1,660
13년 전 조회 763
13년 전 조회 3,847
14년 전 조회 1,094
14년 전 조회 1,970
14년 전 조회 1,448
14년 전 조회 841
14년 전 조회 4,814
14년 전 조회 703
14년 전 조회 2,809
14년 전 조회 1,155
14년 전 조회 820
14년 전 조회 1,194
14년 전 조회 7,962
14년 전 조회 1,126
14년 전 조회 2,209
14년 전 조회 1,659
14년 전 조회 1,213
14년 전 조회 4,281
14년 전 조회 878
14년 전 조회 1,351
14년 전 조회 981
14년 전 조회 1,037
14년 전 조회 1,735
14년 전 조회 2,522
14년 전 조회 1,060
14년 전 조회 1,188
14년 전 조회 1,114
14년 전 조회 1,488
14년 전 조회 718
14년 전 조회 1,193
14년 전 조회 707
14년 전 조회 1,113