jquery数组推送在ajax成功中无法正常工作

jac*_*ack 2 ajax jquery array-push

我正在尝试将一个div id推入一个数组.Array push正在运行良好的bofore ajax调用.但是当我在ajax成功使用push时,当我点击第二个元素时,首先进行推送.这是

使用以下代码进行数组操作(数组内部成功推送)

first click on id="1"  --- resuting array []
second click on id="2"  --- resulting array [1]
second click on id="3"  --- resulting array [1,2]
Run Code Online (Sandbox Code Playgroud)

我的代码

$(document).ready(function() {

    var count = 0;
    var vPool = '';
    arr = [];
    seat = [];
    var totalseat = '<?php echo $sumofseat; ?>';
    var date = ' <?php echo $new_date; ?>';
    $('.custom_checkbox').click(function() {
        pressed = true;
        var prev = $(this).attr('class');
        var arrid = $(this).attr('id');
        var seats = $(this).attr('title');
        count = $('.selected').length;
        if (prev == 'custom_checkbox') {

            //arr.push(arrid);
            //seat.push(seats);
            $.ajax({
                url: "seat_manipulation.php",
                dataType: 'json',
                data: '&operation=save&seat=' + arrid + '&guid=<?php echo $guid; ?>&date=' + date,
                type: "POST",
                context: this,
                success: function(data) {

                    if (data.status == 'SAVED') {
                        $(this).toggleClass('selected');
                        $('#count').slideDown();
                        $('#selecte_seat').show();
                        $('#count').html(count + ' Seats selected');
                        alert(arrid);
                        //if(jQuery.inArray(arrid,arr) == -1) {
                        arr.push(arrid);


                        //}
                        //if(jQuery.inArray(seats,seat) == -1) {
                        seat.push(seats);
                        //}
                    } else {
                        alert("Seat already been used.Please select another");
                    }

                }
            })

        }
    });
});
Run Code Online (Sandbox Code Playgroud)

我错了..或者这就是它的工作方式?提前致谢

Bur*_*imi 5

你需要用" async:false" 来配置你的Ajax ,因为有一个Race Condition的东西,所以在你操作Array的时候阻塞代码.

这个问题.