var myCountyInfo = {
counties: [{
name: 'County 1',
id:123,
locationInfo: {
lat: 453245,
lng: 45545,
avgTemp: '75f',
population: '5 B.'
}
}, {
name: 'County 2',
id:456,
locationInfo: {
lat: 11221,
lng: 542222,
avgTemp: '59f',
population: '2 B.'
}
}, {
name: 'County 3',
id:789,
locationInfo: {
lat: 88555,
lng: 54757,
avgTemp: '58f',
population: '1 B.'
}
}]
}
function populateSelectBoxes($select, data) {
var counties = [];
$.each(data, function() {
counties.push('<option value="'+this.id+'">' + this.name + '</option>');
});
$select.append(counties.join(''));
}
function populateTableRow($tableBody, data, selectedCountyId) {
var county;
$.each(data, function() {
if (this.id == selectedCountyId) {
county = this;
return false;
}
});
$tableBody.html('<tr>'+
'<td>' + county.name + '</td>'+
'<td>' + county.locationInfo.lat +'</td>'+
'<td>' + county.locationInfo.lng + '</td>'+
'<td>' + county.locationInfo.avgTemp + '</td>'+
'<td>' + county.locationInfo.population + '</td>'+
'</tr>');
}
populateSelectBoxes($('#my-select'), myCountyInfo.counties);
$('#my-select').change(function() {
var $this = $(this);
var selection = $this.val();
populateTableRow($('#my-table tbody'), myCountyInfo.counties, selection);
});
td,th{
padding:5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="my-select"></select>
<table id="my-table" border="1">
<thead>
<tr>
<th>County</th>
<th>Lat.</th>
<th>Lng.</th>
<th>Avg Temp</th>
<th>Population</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
像'變種縣= $( 「#myDropdown」 ).VAL(); $(「#county-result」)。load(「county_list.html#」+ county);' –
如果是我,我會使用JSON作爲我的數據源並使用模板填充div並遍歷JSON對象,並尋找所選的ID – DelightedD0D
@ DelightedD0D我同意你,我可能會做同樣的! – Abdel