简体   繁体   English

如何获得动态创建的ID的当前值?

[英]How would I get the current value of a dynamically created Id?

So I'm currently getting the value of a dropdown menu on change, and then adding it to the id of a div that surrounds the dropdown menu. 因此,我目前正在更改中获得下拉菜单的值,然后将其添加到围绕下拉菜单的div的ID中。

I'm wanting to be able to use this value later on in my code but right now it seems that I'm getting an undefined variable probably because it's not assigned on page load. 我希望以后可以在代码中使用此值,但是现在看来我正在获取未定义的变量,可能是因为未在页面加载时分配它。

What would be a good way/better way of getting this value or assigning the value to a variable? 什么是获取此值或将该值分配给变量的好方法/更好的方法? And what would be a good way of reloading or refreshing the page so that it can be updated to the new variable? 什么是重新加载或刷新页面以使其可以更新为新变量的好方法?

I want to use the selectedValue var in the if statement instead of the "Big 12". 我想在if语句中使用selectedValue var而不是“ Big 12”。

The person who suggested it was the same as another question I don't think is understanding my question. 提出该问题的人与我认为不是的另一个问题理解了我的问题。

Any ideas? 有任何想法吗?

$('#team-select').change(function() {
    var teamSelect = $(this).val();
   $('.select-team-menu').attr('id', teamSelect); 

});

var selectedValue = $('.select-team-menu').attr('id');
console.log(selectedValue);

    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 == "Big 12" || gamesAway == "Big 12"){

Wrap the getGames() in another function that you can call when the change event occurs getGames()包装在发生更改事件时可以调用的另一个函数中

$('#team-select').change(function() {
    var teamSelect = $(this).val();
   $('.select-team-menu').attr('id', teamSelect);    
   // call function declared below and pass in new value from <select>
   loadSelectedTeam(teamSelect);
});


// declare function anywhere in file
function loadSelectedTeam(selectedValue){    
    getGames().done(function(results){
       $.each...    
          ....
           // use selectedValue passed into function
           if(gamesHome == selectedValue  || gamesAway == selectedValue ){....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM