jQuery sortable code not behaving like I expect
<p>I'm using jQuery to allow drag-n-drop between two lists, and I'm not getting the behavior I expect.</p>

<p>Each list comes in two pairs with the ids <code>teams_in_set&lt;set-number&gt;</code> and <code>teams_not_in_set&lt;set-number&gt;</code>, all members of the class <code>connected-sortable</code>.  There will be an arbitrary number of these pairings on the page, with an set-number of 0 at the end, representing a new set being created (as opposed to an existing set being edited).</p>

<p>I'm setting it up with the following function in <code>$(document).ready</code>:</p>

<pre><code>
    var teams_in_set_pattern = /^teams_in_set(\d*)$/;
    $(function() {
        var connected_list = $(".connected-sortable");
        for (var i = 0; i &lt; connected_list.length; i++) {
            var id = connected_list[i].id;
            if (teams_in_set_pattern.test(id)) {
                var set_num = id.match(teams_in_set_pattern)[1];
                var teams_in = "#teams_in_set" + set_num;
                var teams_out = "#teams_not_in_set" + set_num;

                if (set_num > 0) {
                    $(teams_in + ", " + teams_out).sortable({
                        revert: true,
                        connectWith: ".connected-sortable", 
                        cursor: 'move', 
                        receive: function(event, ui) {
                            update_teams(set_num);
                        } 
                    }).disableSelection();
                }
                else {
                    $(teams_in + ", " + teams_out).sortable({
                        revert: true,
                        connectWith: ".connected-sortable", 
                        cursor: 'move', 
                    }).disableSelection();
                }
            }
        }
    });
</code></pre>

<p>The idea is that when a team is moved from one list to the other, the <code>update_teams(set_num);</code> line gets called, triggering some Ajax that updates the database.  The problem is the variable <code>set_num</code>; it seems like rather than retain the value it had when <code>receive:</code> was populated, it has the value it was last set to, which always winds up being 0.</p>

<p>What am I misunderstanding?  And what do I need to do to get the behavior I want?</p>
