点击弹出窗口显示错误数据

时间:2017-03-17 22:29:39

标签: javascript html themoviedb-api

我制作了一个供我自己使用的影院时间表页面,但我遇到了问题。 我想让页面弹出我点击的电影的信息,但它总是显示列表中最后一部电影的信息。

我确定它很简单,但我只是看不到它。

我用来获取所需信息的代码:



    var settings = {
  "async": true,
  "crossDomain": true,
  "url": ("https://api.themoviedb.org/3/search/movie?api_key=e8a6a870421f5cc13e775873bfe1cad8&language=bg&query=")+encodeURIComponent(fn),
  "method": "GET",  
  "headers": {},
  "data": "{}"
}
$.ajax(settings).done(function (response) {
    var plakat = response.results[0].poster_path;
  window.movieId = response.results[0].id;
  window.title = response.results[0].title;
  window.poster = "https://image.tmdb.org/t/p/w92/"+plakat;
  window.overview = response.results[0].overview;
  console.log(response.results[0].title);
  var k;
  for(k=0; k< ttest.length; k++){
  document.getElementById('myPopup').innerHTML = ("<img align='left' src="+window.poster+"><h2>"+window.title+"</h2><br>"+window.overview);
  }
}); 
&#13;
&#13;
&#13;

然后我想循环所有结果,以便我可以更改弹出窗口中显示的数据,但它似乎不起作用。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这将显示每部电影的缩略图图像。单击缩略图时,将显示标题和说明(如果可用):

JS

var fn = "snow white";
var settings = {
"async": true,
"crossDomain": true,
"url": ("https://api.themoviedb.org/3/search/movie?api_key=e8a6a870421f5cc13e775873bfe1cad8&language=bg&query=")+encodeURIComponent(fn),
"method": "GET",  
"headers": {},
"data": "{}"
}

$.ajax(settings).done(function (response) {

for(var i = 0; i < response.results.length; i++) {
  var obj = response.results[i];

  var plakat = obj.poster_path;
  var movieId = obj.id;

  var title = obj.title.replace(/'/g, "&#39;");
  var title = title.replace(/"/g, '\\"');

  if (plakat !== null) {
    var poster = "https://image.tmdb.org/t/p/w92/"+plakat;
  } else { 
    var poster = ""; 
  }

  var overview = obj.overview.replace(/'/g, "&#39;");
  var overview = overview.replace(/"/g, '\\"');

  $("#movieList").append("<a href='javascript:showInfo("+'"'+title+'"'+","+'"'+overview+'"'+");'><img src='"+poster+"' height='102' width='82' style='padding:5px;'></a>");

}
}); 


function showInfo(title,overview){
  document.getElementById('myPopup').innerHTML = "<b>"+title+"</b>"+"  <br>"+overview; 
}

HTML

<div id="movieList" align="left"></div>
<div id="myPopup"></div>