function 사용법 질문드립니다.

function 사용법 질문드립니다.

QA

function 사용법 질문드립니다.

본문

form을 쓰지않고 하려고하는데요



<input type="checkbox" name="termsPrivacy" value="">
<input type="checkbox" name="chk_agree" value="">



<a href="#" class="btn_typeW" onclick="return nextStep();">


<script>
function nextStep()
{
2개의 체크박스 체크가 되어지지않으면
alert을 띄우게 하고싶어요.

}

</script>



체크박스가 체크됬는지안됬는지 확인을해서

조건문을줘서 alert을 띄우고싶어요..





이 질문에 댓글 쓰기 :

답변 4

1. jquery를 활용하시는 방법



<script src="https://code.jquery.com/jquery-1.11.3.js"></script> 
<script>
function nextStep()   {
var nextCheck = ($("input[name=termsPrivacy]".checked).length > 0 &&
  $("input[name=chk_agree]".checked).length > 0));
if (!nextCheck)  {
alert("체크박스를 둘다 선택하세요!");
return;
}
}
</script> 


2. 직접 자바스크립트를 이용하는 방법


<script>
function nextStep()   {
var t = document.getElementsByName("termsPrivacy");
var c = document.getElementsByName("chk_agree");


if (t.checked == false || c.checked == false) {
    alert("체크박스를 둘다 선택하세요!");
    return;
}
}
</script>


그외에 방법도 있겠으나

자바스크립트를 사용하는 방법에 따라 달라지는 정도라서 크게 문제될것은 없어보입니다.


<input type="checkbox" name="termsPrivacy" id="termsPrivacy" value="">
<input type="checkbox" name="chk_agree" id="chk_agree" value="">
<a href="#" class="btn_typeW" onclick="return nextStep();">Next Step</a> 
<script> 
function nextStep()
{
	if(document.getElementById('termsPrivacy').checked == false || document.getElementById('chk_agree').checked == false){
		alert('x');
	} else {
		alert('o');
	}
}
</script> 
답변을 작성하시기 전에 로그인 해주세요.
전체 125,873 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT