我正在使用.get()構建季節列表。當我這樣做時,我將<li><a>
附加到我的<ul>
元素上。加載完成後,我想訪問<a>
元素以返回其ID,點擊。但是,當我嘗試這個時,我無法訪問這些元素onclick。循環後訪問元素的ID
<script type="text/javascript">
$(function(){
var org = localStorage.getItem("org");
var key = localStorage.getItem("key");
var user = localStorage.getItem("user");
$.get('https://domain.com/App/GetSeasons/?user='+user+'&key='+key+'&org='+org, function(xml){
var seasons = $.xml2json(xml);
for (i=0; i<=seasons.Season.length; i++){
var seasonName = seasons.Season[i].name;
var SeasonID = seasons.Season[i].id;
$("#seasonlist").append("<li><a href='#' id="+SeasonID+">" + seasonName + "<span class=pull-right>></span></a></li>");
}
});
});
</script>
<script>
$("#seasonlist > li > a").click(function(){
var test = $(this).attr("id");
console.log(test);
});
</script>