2011-07-27 38 views
1

新的jQuery。需要簡單語法的幫助。不確定調用多個函數的正確方式。我只需要簡單的語法幫助

<script> 
$(document).ready(function() { 
    jQuery.validator.addMethod("phoneUS", function (phone_number, element) { 
     phone_number = phone_number.replace(/\s+/g, ""); 
     return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); 
    }, "Please specify a valid phone number"); 
    $("#networx_affiliate").validate({ 
     rules: { 
      service_name: "required" 
     } 
    }).ajaxForm({ 
     url: '**********', 
     target: '#output', 
     type: 'post', 
     dataType: 'xml', 
     success: function processXml(responseXML) { 
      // 'responseXML' is the XML document returned by the server; we use 
      // jQuery to extract the content of the message node from the XML doc 
      $(xml).find("affiliateresponse").each(function() { 
       $("#output").append($(this).find("successCode") + "<br />"); 
       $("#output").append($(this).find("errormessage") + "<br />"); 
      }); 
     } 
    }); 
}); 
</script> 

我得到了驗證表單插件和ajax表單插件。請給他們打電話的正確方法是什麼?導致我現在有他們的方式阿賈克斯似乎並沒有工作。

謝謝

+0

完成謝謝@Brock亞當斯 – mojotaker

回答

0

,如果你想打電話給表單驗證後AJAX方法,你可以這樣

$("#networx_affiliate").validate({ 
     submitHandler: function (form) { 
      CallAjaxMethod(); 
     }, 

     rules: { 
      service_name: "required" 
     } 
}); 

function CallAjaxMethod() 
{ 
    $.ajax({ 
     url: 'https://api.networx.com', 
     target: '#output', 
     type: 'post', 
     dataType: 'xml', 
     success: function processXml(responseXML) { 
      // 'responseXML' is the XML document returned by the server; we use 
      // jQuery to extract the content of the message node from the XML doc 
      $(xml).find("affiliateresponse").each(function() { 
       $("#output").append($(this).find("successCode") + "<br />"); 
       $("#output").append($(this).find("errormessage") + "<br />"); 
      }); 
     } 
    }); 

} 
+0

謝謝,非常感謝。現在對我來說很有意義。但我只是意識到,我不能做ajax crossdomain XML請求。 – mojotaker

0

.validate()返回驗證器而不是表單對象。嘗試先調用.ajaxForm(),然後調用.validate()。

相關問題