2014-06-05 1 views
-1

좋아, 여기 상황이있다, 나는 form를 유효하게하고있다. 이 양식에는 4 개의 입력 상자가 있습니다. 2 개의 유효성 검사가이 양식에서 수행됩니다. 하나의 유효성 검사는 3 개의 상자에 일부 값이 있는지 검사합니다 (parsely으로 수행). 하나의 상자에 몇 가지 코드가 있으며 코드가 유효한지 검사를 실행하려면이 코드를 서버에 보내야합니다. 여기에 내가이 일을하고 어떻게 :먼저 AJAX 결과를 얻는 방법 다음 함수를 실행하십시오

(의사 코드)

onFormSubmit(){ 
    var isValid, 

    //This is a callback and to get the result of ajax. Ajax function is in different file 
    validateCode = function(response){ 
     isValid = response; // true or false 
    }; 

    isValid = $('#form').parsley('validate'); 

    //Calling that other file function for ajax result, arguments are passed as object validateCode is function name. 
    this.emit('validateCode', {value:promoCodeValue, validate:validateCode}); 


    // Here in this function, the value of isValid is not updating properly. It is taking the value given by parsley validation. 
    doOtherStuff(isValid) 

} 

그래서 제 질문은, 내가 완료 아약스 기다렸다가 doOtherStuff 기능을 실행할 수있는 도움으로 어떤 방법이있다 .

답변

0

코드를 약간 재정렬해야합니다. `그래서 문제가 .. 단순히로 이동 실행하는 유효성 검사 코드를 기다리고되지되어 가`(isValid) {doOtherStuff()} 경우 :이처럼 doOtherStuff``에 조건이

onFormSubmit(){ 
    var isValid; 
    //This is a callback and to get the result of ajax. Ajax function is in different file 
    validateCode = function(response){ 
     isValid = response && $('#form').parsley('validate'); // true or false 
     //Calling that other file function for ajax result, arguments are passed as object validateCode is function name. 
     this.emit('validateCode', {value:promoCodeValue, validate:validateCode}); 
     // Here in this function, the value of isValid is not updating properly. It is taking the value given by parsley validation. 
     if(isValid){ 
       doOtherStuff(isValid); 
     } 
    }; 
} 
+0

else condition –

+0

위의 코드에도 문제가 있습니까? 나는 그것을 약간 편집했다. 이 시나리오에서는 Ajax 응답을 기다린 다음 응답 및 자바 스크립트 유효성 검사의 유효성을 확인한 후 doOtherStuff()를 호출합니다. – Karthik

관련 문제