【发布时间】:2018-08-08 17:57:58
【问题描述】:
我正在尝试构建一个提取 JSON 文件的小型 Web 应用程序。我能够获得显示的信息,但我想根据所选值限制显示的内容。
这是我的html
<div class="wrapper">
<div class="profile">
<select>
<option value="default" selected>Choose a superhero...</option>
<option value="1111">Superman</option>
<option value="2222">Batman</option>
<option value="3333">Spiderman</option>
</select>
<button id="showPeopleButton">Show People</button>
<table id="userdata" border="2" style="display: none">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>City</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
现在是我的 JavaScript。我已将 JSON 数据放在 JavaScript 中。
/* Listen for the Show People button to be clicked */
$('#showPeopleButton').click(function() {
// Call the showPeople() function
showPeople();
});
// Json data
var people = [{
"firstName": "Clark",
"lastName": "Kent",
"job": "Reporter",
"roll": 20,
"heroId": 1111
},
{
"firstName": "Bruce",
"lastName": "Wayne",
"job": "Playboy",
"roll": 30,
"heroid": 2222
},
{
"firstName": "Peter",
"lastName": "Parker",
"job": "Photographer",
"roll": 40,
"heroId": 3333
}];
// Show People function will draw the table
function showPeople() {
// Show table
$('#userdata').show();
//Populate table
$.each(people, function(index, person) {
var tableRow =
"<tr>" +
"<td>" + person.firstName + "</td>" +
"<td>" + person.lastName + "</td>" +
"<td>" + person.job + "</td>" +
"<td>" + person.roll + "</td>" +
"</tr>";
$(tableRow).appendTo("#userdata tbody");
});
}
我怎样才能让它为我工作?
【问题讨论】:
-
基于什么价值
-
我要匹配html选项中的“heroId”值。
标签: javascript jquery html json search