尝试使用基于json值的jquery显示图像

时间:2017-07-30 14:00:13

标签: jquery html json ajax

我正在学习使用ajax,我试图让它根据json(sunny loads sun.png等)中的值加载图像,但它不起作用。

也许我不理解整个'回应'。无论如何我完全卡住了 - 显示的图像是一遍又一遍的相同图像(云端图像),尝试了几个小时,也许有人可以提供帮助。

success: function(response)
{
	$('#weather_data').html('');
    	var display = '';
    	$.each(response.weatherReports,function(index) 
    	{
    		var condition= response.weatherReports[index].currentConditions;
    		if(condition='Sleet')
    		{
    			var image= '<img src="weather_icons/sleet.png" alt="sleet">';
    		}
    		if(condition='Cloudy')
    		{
    			var image= '<img src="weather_icons/cloud.png" alt="cloudy">';
    		}
    		
    		/*"Sunny", "Cloudy", "Sunny and Cloudy" , "Heavy Cloud" , "Rainy" , "Heavy Rain" , "Thunderstorm" , "Sleet" , "Hail", "Snowy"*/
    		display+= '<tr><td>' + response.weatherReports[index].city.city_name + '</td>';
    		display+= '<td>' + response.weatherReports[index].currentConditions + ' ' + image + '</td>';
    		display+= '<td>' + response.weatherReports[index].temperature + '&deg;C </td>';
    		display+= '<td>' + response.weatherReports[index].wind.speed + 'mph </td>';
    		display+= '<td>' + response.weatherReports[index].wind.direction + '</td>';
    		display+= '<td>' + response.weatherReports[index].wind.chillFactor + '</td></tr>';
    		});
    		$('#weather_table').append(display);
    		var image='';
},

1 个答案:

答案 0 :(得分:0)

让我们一起排除故障,首先在条件之外添加图像变量并查看它是否显示(可能是未找到资源)。 然后,在控制台中登录currentConditions属性,以查看您的IF语句是否检查了正确的单词。