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년 전 조회 699
13년 전 조회 1,064
13년 전 조회 906
13년 전 조회 1,059
13년 전 조회 1,337
13년 전 조회 934
13년 전 조회 1,199
13년 전 조회 779
13년 전 조회 799
13년 전 조회 1,325
13년 전 조회 3,194
13년 전 조회 752
13년 전 조회 2,187
13년 전 조회 963
13년 전 조회 863
13년 전 조회 1,755
13년 전 조회 1,686
13년 전 조회 693
13년 전 조회 1,597
13년 전 조회 2,027
13년 전 조회 1,437
13년 전 조회 684
13년 전 조회 2,101
13년 전 조회 900
13년 전 조회 678
13년 전 조회 646
13년 전 조회 1,371
13년 전 조회 1,750
13년 전 조회 1,224
13년 전 조회 712
13년 전 조회 849
13년 전 조회 1,336
13년 전 조회 1,290
13년 전 조회 625
13년 전 조회 3,423
13년 전 조회 1,290
13년 전 조회 4,450
13년 전 조회 854
13년 전 조회 1,849
13년 전 조회 932
13년 전 조회 831
13년 전 조회 1,738
13년 전 조회 706
13년 전 조회 1,678
13년 전 조회 764
13년 전 조회 1,549
13년 전 조회 1,075
13년 전 조회 4,136
13년 전 조회 2,134
13년 전 조회 953
13년 전 조회 778
13년 전 조회 1,339
13년 전 조회 982
13년 전 조회 1,107
13년 전 조회 2,201
13년 전 조회 852
13년 전 조회 2,908
13년 전 조회 2,900
13년 전 조회 1,156
13년 전 조회 859
13년 전 조회 2,393
13년 전 조회 986
13년 전 조회 1,238
13년 전 조회 763
13년 전 조회 1,886
13년 전 조회 1,183
13년 전 조회 1,240
13년 전 조회 1,579
13년 전 조회 679
13년 전 조회 3,754
13년 전 조회 1,006
13년 전 조회 1,881
13년 전 조회 1,380
13년 전 조회 751
13년 전 조회 4,729
13년 전 조회 599
13년 전 조회 2,722
13년 전 조회 1,060
13년 전 조회 727
13년 전 조회 1,103
13년 전 조회 7,874
13년 전 조회 1,045
13년 전 조회 2,136
13년 전 조회 1,581
13년 전 조회 1,119
13년 전 조회 4,177
13년 전 조회 789
13년 전 조회 1,268
13년 전 조회 891
13년 전 조회 947
13년 전 조회 1,637
13년 전 조회 2,439
13년 전 조회 971
13년 전 조회 1,081
13년 전 조회 1,034
13년 전 조회 1,401
13년 전 조회 640
13년 전 조회 1,108
13년 전 조회 612
13년 전 조회 1,026
🐛 버그신고