【发布时间】:2017-12-18 00:08:33
【问题描述】:
如何在html dom中打印json数据?
我有电影 json,我附加到选择标签,我想在选择电影时将电影名称附加到锚标签...
下面是我的html
<div class="UserData">
<h1><a href="moviebooking.html">MyMovie-Ticket-Booking</a></h1>
<select class="selectCity" id="selectCity">
<option value="City">Select City</option>
<option value="Bengaluru">Bengaluru</option>
<option value="Hyderabad">Hyderabad</option>
<option value="Guntur">Guntur</option>
<option value="Ongole">Ongole</option>
</select>
<span id="welcome"> </span>
</div>
下面是js
$(document).ready(function() {
var cityData = [{
cityName: 'Bengaluru',
value: "Bengaluru",
data: [{
movieName: 'ABC',
theaterName: 'Tulsi Theatre'
},
{
movieName: 'DEF',
theaterName: 'PVR'
},
{
movieName: 'GHI',
theaterName: 'Srinivasa Theatre'
}
]
},
{
cityName: 'Hyderabad',
value: "Hyderabad",
data: [{
movieName: '123',
theaterName: 'Theatre1'
},
{
movieName: '456',
theaterName: 'PVR2'
},
{
movieName: '789',
theaterName: 'Theatre3'
}
]
},
{
cityName: 'Guntur',
value: "Guntur",
data: [{
movieName: 'ABC1',
theaterName: 'Theatre4'
},
{
movieName: 'DEF2',
theaterName: 'PVR3'
},
{
movieName: 'GHI3',
theaterName: 'Theatre5'
}
]
},
{
cityName: 'Ongole',
value: "Ongole",
data: 'currently not available'
}
];
$("#selectCity").on('change', function() {
var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
var locationString = '';
var locationString2 = '';
console.log(locations)
$.each(locations, function(i, item) {
console.log(JSON.stringify(item));
locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
});
$('#secondselectbox').html(locationString);
$('#thirdselectbox').html(locationString2);
});
});
在选择标签中选择电影名称后如何附加剧院名称和电影名称......
以及如何分别过滤电影和剧院.....
【问题讨论】:
-
你在哪里卡住了?到目前为止,您尝试过做什么?与此同时,您似乎还没有开始。尝试独自解决挑战,当您遇到困难,并且在 SO 中找不到任何其他答案时,请提出具体问题
标签: javascript jquery html json