我基本上是从 itunes rss 提要中提取信息,以根据从 select 元素中选择的国家/地区显示热门歌曲。
我试图替换所选选项的值并将其插入 rss url 以获取该国家/地区的热门歌曲。数据很好但是我 值得注意的是选择任何其他选项。日本是唯一显示的,除非我更改第一个选项。我如何获得更改显示数据的选项?
// selects value for chosen country
var country;
//its selecting the value however its not refreshing.
country = $("#country :selected").text();
$.get('https://itunes.apple.com/' + country + '/rss/topsongs/limit=3/xml', function(data) {
var songArray = $(data).find('entry');
songArray.each(function() {
var title = $(this).find("title").text(); //grab song title
var artist = $(this).find("im\\:artist, artist").text(); //grab artist name
var album = $(this).find("im\\:name,name").text(); //grab album name
var image = $(this).find("im\\:image,image").eq(2).text(); //grab image link
var audioLink = $(this).find("link").eq(1).attr("href"); //grab music file
var audioDiv = $("source").attr("src", "audioLink"); //adds audio to source div
audioLink = '"' + audioLink + '"'
// youtube api to provide link to video based on the top songs populated
// Set the search term
var searchTerm = title;
var link;
var url = "https://www.googleapis.com/youtube/v3/search?q=" + searchTerm + "&part=snippet&maxResults=1&key=AIzaSyBvccDrp39n-InLrjDvv4PH_vfNbN0J_iE";
$.getJSON(url, function(data) {
var id = data.items[0].id.videoId;
link = "https://www.youtube.com/watch?v=" + id;
$("#song").append(
" <br><br>Song :" + title + "<br> Artist :" + artist + " <br>album: " + album + "<br>" + "<img src=" + image + "> " + "<a href= " + link + ">" + link + "</a> <br>" + audioLink
);
});
}, "xml");
});
<select name="country" id="country">
<!-- <option>select song</option> -->
<option value="japan">jp</option>
<option value="spain">es</option>
<option value="france">fr</option>
</select>
<input type="button" id="button" value="get songs">
最佳答案
您需要获取 selected
选项的 value
或 text
var country;
//its selecting the value however its not refreshing.
country= $("#country option:selected").text();
或者如果你想要值
你可以这样做
country= $("#country").val();
关于javascript - 根据选择选项更改显示哪个国家/地区的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41054219/