0

これは非常に簡単だと思います。別のページにデータを投稿する作業中の AJAX Jquery スクリプトがあります。入力を含む部門にプリローダーを追加したいと思います。各部門には、ID を持つクラス名があります。

beforeSend 関数内で投稿データを使用する方法を知る必要があります。

$('#stock_ctrl input').change(function() {

        $.ajax({
            type: "POST",
            data:   {   stock: this.value, 
                        id: this.name
                    },
            url: "example.php",

            beforeSend: function(data) {

                $('.updating'+data.id).html("Preloader here.");

               // I have tried data.id, this.name, $(this).attr.("name");

            success: function(){
                // success (this works fine)
            }
        });

    });

コードのコメントを参照してください。これは、元の入力から取得した名前データを使用したい場所であり、これが私がこだわっているものです。どんな助けでも大歓迎です。

4

3 に答える 3

1

beforeSend の無名関数が実行されているときに、コンテキストを切り替えるため、thisは同じオブジェクトを参照しなくなります。this関数に必要なものを与える必要があります

$('#stock_ctrl input').change(function() {

    $.ajax({
        type: "POST",
        data:   {   stock: this.value, 
                    id: this.name
                },
        url: "example.php",

        beforeSend: $.proxy(function(data) {

           // this should now refer to the correct object and you can perform your operations
        }, this),
        success: function(){
            // success (this works fine)
        }
    });

});

入力しただけですが、動作するはずです

于 2012-05-14T13:53:35.127 に答える
1

ajax を送信する前に $(this) を定義します。次に例を示します。

$('#stock_ctrl input').change(function() {  
var self = $(this);

    $.ajax({
        type: "POST",
        data:   {   stock: self.val(), 
                    id: self.attr('name')
                },
        url: "example.php",
        beforeSend: function(data) {
            $('.updating'+data.id).html("Preloader here.");
           // I have tried data.id, this.name, self.attr.("name");
        success: function(){
            // success (this works fine)
        }
    });
});

それは仕事をするべきです:)

于 2012-05-14T13:52:03.643 に答える
0

これはそれを行う必要があります

$('#stock_ctrl input').change(function() {
    var myVar = $(this);
    $.ajax({
        type: 'post',
        data: {
            stock: myVar.val(),
            id: myVar.attr('name')
        },
        url: 'example.php',
        beforeSend: function(data) {
            //do anything with myVar
        },
        success: function(){
            //do anything with myVar
        }
    });
});
于 2012-05-14T13:53:42.070 に答える