본문 바로가기

에러

검증에서 반복문 사용시 submit return 적용

    $("#updateBtn").click(function(){

    //검증

    $(".screenid").each(function(index){

    if($(".screenid:eq("+index+")").val()==""){

    alert("화면 아이디를 모두 입력해주세요.");

    $(".screenid:eq("+index+")").focus();

    return;

    }

    });

    });

    $("#detailCreateFrm").submit();


위의 예제를 보면


JQeury의 each반복문을 이용하여 검증하고 있다. 이때 if 문으로 검증하고 return; 을 해줬을 경우 검증 자체를 빠져나올 줄 알았지만


반복문만 빠져나올 뿐 아래로 계속 프로그램을 실행하더라..


결국 submit을 만나고야 마는데..



아래와 같이 해결해 주었다.


    $("#updateBtn").click(function(){

     var b=false;

     //검증

     $(".screenid").each(function(index){

     if($(".screenid:eq("+index+")").val()==""){

     alert("화면 아이디를 모두 입력해주세요.");

     $(".screenid:eq("+index+")").focus();

     b = true;

     }

     });

     if(b) return;

    });

    $("#detailCreateFrm").submit();