jQuery覆盖$ .post函数

首先,我为我可怜的英语道歉…希望有人能理解我的问题并帮助我……

我正在使用大量的$ .post调用项目,我希望通过为所有这些调用添加相同的validation来改进它们。

我不想一个接一个地更改所有脚本,所以有没有办法覆盖$ .post函数同时向所有脚本添加相同的东西? 或者也许是一种在每个$ .post上触发另一个函数的方法? 就像是 :

$.post.each(function(){[...]}); 

要么

 $('body').on('post', function(){[...]}); 

谢谢 !

编辑:最后按我的意愿做了! 这是我的代码:

 // Override $.post (function(){ // Store a reference to the original remove method. var originalPostMethod = jQuery.post; // Define overriding method. jQuery.post = function(data){ // Execute the original method. var callMethod = originalPostMethod.apply( this, arguments); callMethod.success(function(){ //console.log('success'); }); callMethod.error(function(){ //console.log('error'); }); callMethod.complete(function(){ //console.log('complete'); return callMethod; }); } })(); 

 $.post = function() { var _method = $.post; // do something you want console.log("do someting"); return _method.apply($, Array.prototype.slice.apply(arguments)); } 

您可以创建一个jQuery-Plug,它执行validation,然后将数据发布到服务器。 基础知识可以在jQuery 教程中找到。