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>
function nextStep()
var chk1= document.getElementsByName("termsPrivacy").checked;
var chk2= document.getElementsByName("chk_agree").checked;
if (chk1 == false || chk2 == false) {
alert("");
} else {
alert("");
}
저도
alert(document.getElementsByName("termsPrivacy").checked);
alert(document.getElementsByName("chk_agree").checked);
이런식으로 체크해보면 될것입니다
답변을 작성하시기 전에 로그인 해주세요.