0

jQuery UI 1.9 マイルストーン 7 の時点で、jQuery UI スピナーのベータ版を使用しています。 jQuery セレクター (.spinner)。これらのスピナーは、一連のオブジェクトの値を設定しています。これらのオブジェクトはすべて、リソースの同じ「プール」から取得されます。スピナーの値が更新されるたびに、そのリソース プールの残り量に基づいて、すべてのスピナーの最大値を調べて更新したいと考えています。これまでの基本的なコードは次のとおりです。

var unusedPool = 500;

jQuery(document).ready(function($) {
    $('.spinner').spinner({
        min: 0,
        max: (unusedPool + this.value),
        change: function(event, ui) {
            // An AJAX call which pushes the change to the DB,
            // and returns a JSON object containing the change in what remains
            // in the resource pool.
            jQuery.ajax({
               // URL, type, etc. etc.
               data: { newValue: this.value },
               success: function (data) {
                   unusedPool -= data.change;
                   // Here is where I need to go through and update *all* spinners,
                   // so their new "max" is their current value plus the new 
                   // unusedPoolSize.
               }
            }   
        }
});

私がやろうとしていることをする方法はありますか?イベントをすべてのスピナーにバインドしてからそのイベントをトリガーする必要がありますか、またはすべてのスピナーにアクセスして「最大」値を変更するためのより直接的な方法 (変更コールバック内) はありますか?

4

2 に答える 2

0

Kvamの答えは非常に近いことがわかりましたが、success関数内の更新はspinner.changeコールバック内には入りません(.ajax呼び出し自体が.changeコールバック内にあるため)。私は最終的にこれが仕事をすることを発見しました:

var unusedPool = 500;

jQuery(document).ready(function($) {
    var spinners = $('.spinner').each(function() {
        $(this).spinner({
            min: 0,
            max: (parseInt(this.value) + unusedPool),
            change: function(event, ui) {
                jQuery.ajax({
                    data: { newValue = this.value },
                    success: function(data) {
                        unusedPool -= data.change;
                        spinners.each(function() {
                            var newMax = parseInt(this.value) + unusedPool;
                            $(this).spinner({ max: newMax });
                        }
                    }
                });
            }
        });
    });
于 2012-05-15T18:35:57.967 に答える
0

success 関数に次のようなものを追加できると思います。

$('.spinner').each(function() {
  $(this).spinner().change(function() {
    $(this).spinner('option', 'max', $(this).spinner('value') + unusedPool);
  });
});
于 2012-05-14T19:59:57.160 に答える