4

まず第一に、私の拙い英語をお詫びします...誰かが私の質問を理解して助けてくれることを願っています...

私は多くの $.post 呼び出しを使用するプロジェクトに取り組んでおり、それらすべてに同じ検証を追加して改善したいと考えています。

すべてのスクリプトを 1 つずつ変更したくないので、$.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;
        });
    }
})();
4

2 に答える 2

2
$.post = function() {
  var _method = $.post;
  // do something you want
  console.log("do someting");
  return _method.apply($, Array.prototype.slice.apply(arguments));
}
于 2012-08-13T08:09:55.013 に答える
0

検証を行い、データをサーバーに投稿する jQuery-Plug を作成できます。基本は jQueryチュートリアルにあります。

于 2012-08-13T08:07:17.097 に答える