html

체크박스 비활성화

SojuMan 2009. 2. 22. 17:39

우선 PHP로 CheckBox에 Check가 되어 있지 않다면 SelectBox에 disabled="disabled"이 출력되도록 하세요.

참고로 SelectBox의 Option 엘리먼트에 selected도 selected="selected"라고 해줘야 합니다.

그리고 SelectBox에 id 속성 값도 부여하세요.

이는 자바 스크립트의 getElementById()를 사용하여 SelectBox에 접근하기 위함입니다.

분홍색으로 표시한 pnum은 PHP에서 $pnum 변수 값을 출력하는 부분입니다.

onclick 부분 소스

 

// el 변수에 user_add10_pnum 아이디의 SelectBox 엘리먼트를 저장합니다.

el = document.getElementById( 'user_add10_pnum' );

 

// this, 즉 CheckBox가 체크가 되어 있으면 el, 즉 SelectBox를 활성화합니다.

if( this.checked ) el.disabled = false;

 

// 체크가 되어 있지 않으면 비활성화합니다.

else el.disabled = true;

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />
<title>...</title>
</head>
<body>
<form action="#">
 <input
  type="checkbox"
  name="user_add9_pnum"
  onclick="el=document.getElementById('user_add10_pnum');if(this.checked)el.disabled=false;else el.disabled=true;"
 />
 <select
  id="user_add10_pnum"
  name="user_add10_pnum"
  disabled="disabled"
 >
  <option>Value 1</option>
  <option>Value 2</option>
  <option>Value 3</option>
 </select>
</form>
</body>
</html>