2013-07-01 3 views
0

한 폼에서 나이를 검증하려고 시도했습니다.이 문제는 크롬 콘솔에서 발견되었습니다. 내가 코드의 다른 곳의 URL 재배치를 넣어 경우 (같은 조건의 상단에)가 잘 작동하기 때문에, 내 코드와 함께 할 수있는 뭔가가 궁금리소스 자바 스크립트 window.location을로드하지 못했습니다.

<script type="text/javascript"> 
    function CalAge() { 

    var dd = $("#day").val(); 
    var mm = $("#month").val(); 
    var yy = $("#year").val(); 
    var age = 18; 

    var mydate = new Date(); 
    mydate.setFullYear(yy, mm-1, dd); 

    var currdate = new Date(); 
    currdate.setFullYear(currdate.getFullYear() - age); 

    if(dd !=0 || mm !=0 || yy !=0){ //whether one or all values havent been choosen 
     if ((currdate - mydate) < 18){ 
      window.location = "http://www.google.com"; 
     } 
     else{ 
      alert("more than 18"); 
     } 
    } 
    else{ 
     alert("please register"); 

    } 
    } 
</script> 

: 이 내 코드입니다. window.location.assign("http://www.google.com") 또한 하나의 else의 모든 팝업을 경고하기 위해 사용 오류에 주어진 좋은 설명의 어떤 제안

+0

시도해보십시오. window.location.href = "http://www.google.com"; ' – Barmar

+0

가 이미 실행되었지만 작동하지 않습니다. – pattpiuf

답변

-1

... 나는이 같은으로 window.location를 교체해야 생각! 선택 사항 인 경우 ... 행운을 빌어 요! Oookay! 이 옵션을 사용합니다 : ` 기능 CalAge() {if ((currdate - mydate) < 18)이 true로 평가되지 때문입니다

 var dd = $("#day").val(); 
    var mm = $("#month").val(); 
    var yy = $("#year").val(); 
    var age = 18; 

    var mydate = new Date(); 
    mydate.setFullYear(yy, mm-1, dd); 

    var currdate = new Date(); 
    currdate.setFullYear(currdate.getFullYear() - age); 

    if(dd !=0 || mm !=0 || yy !=0){ //whether one or all values havent been choosen 

    var subit = currdate - mydate; 
     if (subit < 18){ 
      window.location.href = "http://www.google.com"; 
     } 
     else{ 
      alert("more than 18"); 
     } 
    } 
    else{ 
     alert("please register"); 

    } 
    } 
</script>` 
    thanks good luck 
+0

도 작동하지 않았습니다. 내 코드에서 다른 것을 바꿔야한다고 생각해. 왜냐하면 전혀 작동하지 않기 때문입니다. 다시 한 번 감사드립니다. – pattpiuf

0

합니다.

예에서 currdate은 18 세가되는 컷오프 날짜입니다. 다음,

입니다
if (currdate < mydate) { ... } 

사람이 컷오프 날짜 이후에 태어난 경우 (즉, 18 세 미만이다) Google에 리디렉션 : 귀하의 상태가되어야한다.

관련 문제