根据JSON响应显示隐藏Div
问题描述:
我有两个div-divOne和divTwo。根据JSON响应显示隐藏Div
divOne始终可见,而divTwo隐藏。
现在我读它返回下列数据的JSON服务:
{"ip":"xx.xx.xxx.xxx","country_code":"US","country_name":"United States","region_code":"MI","region_name":"Michigan","city":"Lansing","zipcode":"48917","latitude":42.7257,"longitude":-84.636,"metro_code":"551","area_code":"517"}
我想读的COUNTRY_CODE,如果它是“IN”然后隐藏divOne和显示divTwo。
我该怎么做?这是我迄今为止在jQuery的1.11:
<script>
(function() {
var locAPI = "";
$.getJSON(locAPI)
.done(function(data) {
$.each(data.items, function(i, y) {
}
});
});
})();
</script>
答
<script>
(function() {
var locAPI = "";
$.getJSON(locAPI)
.done(function(data) {
if (data.country_code == 'IN'){
$('#div1_id').hide();
$('#div2_id').show();
}
});
});
})();
</script>
国家代码将可以访问由data.country_code
答
country_code
可以直接访问
if (data.country_code == 'IN'){
divOne.hide();
divTwo.show();
}