HTML& CSS/HTML

고급 - <input> 태그의 type="image" 를 사용할 때 주의할 점

본클라쓰 2009. 8. 3. 12:10

 

<input>태그의 type 속성 중 "image" 속성값은 기본적으로 서밋(submit) 기능을 가지고 있다. 타입 속성을 "image"로 만들면 서밋이 일어나기 때문에 입력폼에서 버튼을 "image"속성을 주어 사용할 경우 주의가 필요합니다.

 

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">


 <script type="text/javascript">
 <!--
 function submit(form) {
  if( !form1.field1.value ) {
   alert("입력필드1을 확인하세요");
   form1.field1.focus();
   return false;
  }
  
  if( !form1.field2.value ) {
   alert("입력필드2을 확인하세요");
   form1.field2.focus();
   return false;
  }
 }

 // 이 함수를 수정하면서 image 타입의 submit 속성을 알아본다.
 // form1.field1.value 값 일 경우 submit
 // form1.field1.value을 form1.field2.value로 수정하면 submit 되지 않음
 function check() {
  if( !form1.field1.value ) {
   alert("입력필드을 확인하세요");
   form1.field2.focus();
   return false;
  }
 }
 
 -->
 </script>


 <title>Insert title here</title>
 </head>
 
<body>


    <form name="form1" action="test1.jsp" onSubmit="return submit(this);">
        <input type="text" name="field1" />
        <input name="button1" type="image" onclick="return check();" value="체크1" src="btn_ok.gif"><p />
        <input type="text" name="field2" />
        <input name="button2" type="image" value="체크2" src="btn_ok.gif">
    </form> 
 
</body>
</html>

 

check()함수로 입력폼 전부를 체크한다면 서밋(submit)이 일어나지 않지만 하나의 입력폼만 체크한다면 그 입력폼에 값이 있는 순간 서밋(submit)이 일어납니다. 따라서 타입속성에 "image"값을 사용할 때는 주의를 기울려야 합니다.