2011-03-07 5 views
0

양식이 있고 j Query validate 플러그인을 사용하여 양식의 유효성을 검사하고 있습니다. 유효성 검사 완료 후 양식 제출을 위해 아약스 코드를 호출하고 싶습니다. jquery 호출 양식 유효성 검사가 완료된 후 ajax 제출

어떻게 작업을 수행합니다

// CALL THE postform code if the form validation is complete? 
// THIS IS THE FORM VALIDATION CODE 
    $(document).ready(function() { 
    $("#myForm").validate({ 
     rules: { 
      password: "required", 
     password_confirm: { 
     equalTo: "#password" 
     }     
     }     
    }); 
    }); 


// THIS IS THE AJAX FORM SUBMISSION CODE 
// I WANT THIS CODE EXECUTED IF CODE ABOVE IS OK 
function postform() 
{ 
    dataString = $("#myForm").serialize(); 
    $.ajax({ 
     url: 'https:/www.someothersite.com'+dataString, 
     type: 'GET', 
     dataType: 'html', 
     timeout: 9000, 
     error: function(){ 
      alert('Error posting to other site'); 
     }, 
     success: function(html){ 
      window.location.href="http://www.mysite.com/?action=success"; 
     } 
    }); 
    return false; 
} 

답변

2

이 방법이 유용 할 것입니다.

$(document).ready(function() { 

    $('#myForm').validate({ 
     rules: { 
      password: 'required' 
     }, 
     password_confirm: { 
      equal_to: 'password' 
     }, 
     submitHandler: function() { 
      var dataString = $(this).serialize(); 

      $.ajax({ 
       url: 'https://www.someothersite.com' + dataString, 
       type: 'GET', 
       dataType: 'html', 
       timeout: 9000, 
       error: function() { 
        alert('Error posting to other site'); 
       }, 
       success: function(html) { 
        window.location.href = 'https://www.mysite.com/?action=success'; 
       } 
      }); 
     } 
    }); 

}); 
+0

감사합니다. – IntricatePixels

+0

문제는 없지만 득표를해도 좋습니다! : D – CodyRob

+0

이것은 내가 한 짓처럼 보이지만 내 이유가 무엇이든 작동하지 않습니다 ... – SoftwareSavant

3

그것은 바로 거기 documentation에서입니다.

$("#myForm").validate({ 
    submitHandler: function(form) { 
     postForm(); 
    } 
}) 
관련 문제