我如何在ajax成功上刷新特定的东西?

时间:2018-01-10 18:51:27

标签: javascript jquery json ajax

我正试图在成功的ajax通话中刷新我的总赌注数,但我不能为我的生活弄清楚如何做到这一点。我确信它并不像我制作它那么复杂。所以我试图在发送ajax调用后从我的JSON doc获取新的pointTotal。

现在它获取了所有更新的信息,但我想要特定游戏的新点总数(每页有多个)。这似乎对我来说应该很容易,但我已经尝试了很多东西而且不能让它发挥作用。你知道怎么做吗?

getGames().done(function(results){
    $.each(results, function (i, gameData){
        $.each(gameData, function(key, game){

            var gamesHome = game.home_team_conference;
            var gamesAway = game.away_team_conference;

            if(gamesHome == selectedValue || gamesAway == selectedValue){

                var gameId = game.id;
                var homeTeam = game.home_team.market;
                var awayTeam = game.away_team.market;
                var pointTotal = game.total_points_bet;
                var gameTime = game.game_time_hour;
                var gameDate = game.game_time_date;
                var homeId = game.home_team.id;
                var awayId = game.away_team.id;
                var network = game.broadcast_network;
                var homePoints = game.total_points_bet_on_hometeam;
                var awayPoints = game.total_points_bet_on_awayteam;
                var totalPoints = homePoints + awayPoints;
                var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
                var hueTwo = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';


            $('.wrapper').append('\
                <div id="'+ gameId +'" class="main-wrapper col-lg-6 col-md-6 col-sm-12">\
                <div class="game-cards">\
                <div class="chart-container">\
                <canvas id="'+ homeTeam +'" width="500" height="500"></canvas>\
                </div>\
                <div class="right-info">\
                <h4>' + awayTeam + '<br>' + " @ " + '<br>' + homeTeam +'</h4>\
                <h5 id="time-channel">'+ gameDate +' @ ' + gameTime  + '<br>' + ' On ' + network +'</h5>\
                <div class="total-points-live">\
                <h5>Total Points Bet</h5>\
                <h5 class="total-points" id="point-total">'+ totalPoints +'</h5>\
                <p>'+ awayTeam +'</p>\
                <input class="bet-input-away" data-away-id="'+ awayId +'" data-team-type="'+ awayTeam +'" type="number" pattern="[0-9]*" name="betAmountAway" placeholder="Wager Amount">\
                <p>'+ homeTeam +'</p>\
                <input class="bet-input-home" data-home-id="'+ homeId +'" data-team-type="'+ homeTeam +'" type="number" pattern="[0-9]*" name="betAmountHome" placeholder="Wager Amount">\
                <p class="bet-button" gameid="'+ gameId +'">Click To Place Bet</p>\
                </div>\
                </div>\
                </div>\
                ');


            $('.bet-input-away').on('input', function() {
               if($(this).val().length)
                  $('.bet-input-home').prop('disabled', true);
               else
                  $('.bet-input-home').prop('disabled', false);
            });
            $('.bet-input-home').on('input', function() {
               if($(this).val().length)
                  $('.bet-input-away').prop('disabled', true);
               else
                  $('.bet-input-away').prop('disabled', false);
            });


            var ctx = document.getElementById(homeTeam).getContext('2d');

            var myChart = new Chart(ctx, {
              type: 'doughnut',
              data: {
                labels: [homeTeam, awayTeam],
                datasets: [{
                  backgroundColor: [
                    hue,
                    hueTwo
                  ],
                  data: [homePoints, awayPoints]
                , borderWidth: 0
                }]
              },
              options: {
                    responsive: true
                ,   maintainAspectRatio: true
              }
            });

            }

        });

    });
});

$('.wrapper').on('click', '.bet-button', function() {
    var self = $(this);
    var gameId = self.attr('gameid');
    var awayVal = $('#' + gameId + ' input[name=betAmountAway]').val();
    var homeVal = $('#' + gameId + ' input[name=betAmountHome]').val();
    var awayId = $('#' + gameId + ' .bet-input-away').data('away-id');
    var homeId = $('#' + gameId + ' .bet-input-home').data('home-id');
    var pointTotals = $('#' + gameId + ' .total-points').val();
    console.log(pointTotals);
    var value = awayVal || homeVal;
    var id, value;


    if (awayVal) {
      id = awayId;
      value = awayVal;
    }
    if (homeVal) {
      id = homeId;
      value = homeVal;
    }

    if (!value) {
        alert('please enter a value!')
    } else {
        $.ajax({
          url: "---------" + userId + "/"+ gameId +"/"+ id +"/"+ value +"",
          type: "get",
        success: function(response) {

            // alert("YOU SUCCESSFULLY SUBMITTED A BET!")

            function update(){

                var currentSelection = $('#team-select').val();

                getGames().done(function(results){
                    $.each(results, function (i, gameData){
                        $.each(gameData, function(key, game){
                            var gamesHome = game.home_team_conference;
                            var gamesAway = game.away_team_conference;

                            if(gamesHome == currentSelection || gamesAway == currentSelection){
                                var gameId = game.id;
                                var homeTeam = game.home_team.market;
                                var awayTeam = game.away_team.market;
                                var pointTotal = game.total_points_bet;
                                var gameTime = game.game_time_hour;
                                var gameDate = game.game_time_date;
                                var homeId = game.home_team.id;
                                var awayId = game.away_team.id;
                                var network = game.broadcast_network;
                                var homePoints = game.total_points_bet_on_hometeam;
                                var awayPoints = game.total_points_bet_on_awayteam;
                                var totalPoints = homePoints + awayPoints;
                                var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
                                var hueTwo = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';





                                // $('#point-total').append(homePoints + awayPoints);

                            }

                        });
                    });
                })
            }

            update();


            $('#' + gameId + ' input[name=betAmountHome]').val(''); //This resets the value box
            $('#' + gameId + ' input[name=betAmountAway]').val(''); //This resets the value box


        },
          error: function(xhr) {
            console.log(xhr)
          }
        });

0 个答案:

没有答案