function 사용법 질문드립니다. 채택완료

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


Copy
<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개

채택된 답변
+20 포인트

1. jquery를 활용하시는 방법


Copy
<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. 직접 자바스크립트를 이용하는 방법

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


그외에 방법도 있겠으나

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

로그인 후 평가할 수 있습니다

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

저도

alert(document.getElementsByName("termsPrivacy").checked);

alert(document.getElementsByName("chk_agree").checked); 

이런식으로 체크해보면 될것입니다



로그인 후 평가할 수 있습니다

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

Copy
function nextStep()var chk1= document.getElementsByName("termsPrivacy").checked;var chk2= document.getElementsByName("chk_agree").checked; if (chk1 == false || chk2 == false) { alert("");} else { alert("");}
로그인 후 평가할 수 있습니다

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

Copy
<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>
로그인 후 평가할 수 있습니다

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

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

로그인
🐛 버그신고