如何获得ajax调用成功时下拉的当前值?

时间:2018-01-10 00:34:26

标签: javascript jquery html ajax

我正在尝试获取下拉菜单的值,以便我可以在我的代码中稍后使用该值作为过滤器,但是现在它正在记录下拉菜单的第一个值,即America East。当我点击我的提交按钮时,我将下拉设置为Big 12,但它始终记录了America East。谁能向我解释我做错了什么?

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


// This checks is the user information
var userId = user.example.id;

console.log(user.example.money);

$('#points-left').append(user.example.money);
$('#point-total-mobile').append(user.example.money);



// This is the click function that sends the bet to the server. 
$('.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 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: "https://---------" + "/"+ gameId +"/"+ id +"/"+ value +"",
          type: "get",
          success: function(response) {

            alert("YOU SUCCESSFULLY SUBMITTED A BET!")
            function update(){
                getGames().done(function(results){
                    $.each(results, function (i, gameData){
                        $.each(gameData, function(key, game){
                            console.log(currentSelection)

                        });
                    });
                })
            }

            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)
          }
        });
    }
});

以下是供参考的HTML:

<div class="select-team-menu">
    <select id="team-select">
       <option value="America East">America East</option>
       <option value="Atlantic 10">Atlantic 10/option>
       <option value="ACC">ACC</option>
       <option value="American Athletic">American Athletic</option>
       <option value="Atlantic Sun">Atlantic Sun</option>
       <option value="Big 12">Big 12</option>
       <option value="Big East">Big East</option>
       <option value="Big Sky">Big Sky</option>
       <option value="Big South">Big South</option>
       <option value="Big Ten">Big Ten</option>
       <option value="Big West">Big West</option>
       <option value="Colonial">Colonial</option>
       <option value="Conference USA">Conference USA</option>
       <option value="Horizon">Horizon</option>
       <option value="Ivy">Ivy</option>
       <option value="MAAC">MAAC</option>
       <option value="Mid-American">Mid-American</option>
       <option value="MEAC">MEAC</option>
       <option value="Missouri Valley">Missouri Valley</option>
       <option value="Mountain West">Mountain West</option>
       <option value="Northeast">Northeast</option>
       <option value="Ohio Valley">Ohio Valley</option>
       <option value="Pac-12">Pac-12</option>
       <option value="Patriot League">Patriot League</option>
       <option value="SEC">SEC</option>
       <option value="Southern">Southern</option>
       <option value="Southland">Southland</option>
       <option value="SWAC">SWAC</option>
       <option value="Summit League">Summit League</option>
       <option value="Sun Belt">Sun Belt</option>
       <option value="West Coast">West Coast</option>
       <option value="WAC">WAC</option>
    </select>
</div>

1 个答案:

答案 0 :(得分:2)

您似乎在代码的开头设置console.log($('#team-select').val()),这只是将其设置为当前选定的项目。它永远不会改变。您需要将日志更改为stringdist以获取更新后的值。