채택완료

체크박스 동의 여부 스크립트 문의

2017-08-04 (금) 02:13:14 3,636

안녕하세요. 아래 처럼 소스를 짯는데 체크박스 부분만 얼럿창이 안되네요 ㅜ

고수님들 도와주삼요..

Copy
<form name="f1" method="post" enctype='multipart/form-data'>약관의 내용에 동의합니다.<input type="checkbox" name="agree" value="1" id="agree"><input type="button" value="참가신청하기" onclick="form_Check();"></form>​<script>function form_Check(){if (!f.agree.checked) {             alert("약관 내용에 동의해주세요.");             f.agree.focus();             return false;         }  if(f1.name.value == ''){  alert("이름을 입력해주십시오.");  f1.name.focus();  return; }...</script>

|

답변 2개

채택된 답변
+20 포인트
Copy
<form name="f1" method="post" enctype='multipart/form-data' onsubmit="form_Check()">약관의 내용에 동의합니다.<input type="checkbox" name="agree" value="1" id="agree"><input type="submit" value="참가신청하기"></form>​<script>function form_Check(){	if (!f1.agree.checked) { 		alert("약관 내용에 동의해주세요."); 		f1.agree.focus(); 		return false; 	} 	if(f1.name.value == ''){	  alert("이름을 입력해주십시오.");	  f1.name.focus();	  return false;	 }}</script>

오타가 있네요

Copy
if (!f.agree.checked) {

부분에 

f1이 되어야 할것 같구요


더 잘 작동하게 하시려면

Copy
<script>
function form_Check(){
    var f = document.f1;
    if (!f.agree.checked) { 
        alert("약관 내용에 동의해주세요."); 
        f.agree.focus(); 
        return false; 
    } 

    if (f.name.value == ''){              
       alert("이름을 입력해주십시오.");
       f.name.focus();
       return;
    }

.
.
.

</script>
 
와 같이 f1을 f로 지정하시고
전부 f로 바라보게 하는것도 방법입니다.

document 바로 하위에 form의 name="f1" 을 가지는것을 지정할때 주로 사용합니다

가급적 중복되지 않는 이름을 사용하시는게 더 좋습니다.




답변을 작성하려면 로그인이 필요합니다.