我正在尝试构建一个提取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");
});
}
我怎样才能让它为我工作?
答案 0 :(得分:3)
你必须
每次调用showPeople时重置英雄表。
将heroid属性名称更改为heroId for batman:P
过滤people数组以仅匹配英雄ID 目前已选定
显示结果
示例代码:
/* Listen for the Show People button to be clicked */
$('#showPeopleButton').click(function() {
// Call the showPeople() function
showPeople();
});
var heroes = [{id: 1111, name: 'Superman'}, {id: 2222, name:'Batman'}, {id: 3333, name: 'Spiderman'}];
// 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();
// Reset table
$("#userdata tbody").empty();
//Populate table
var heroId = Number($(".profile select").val());
var filteredPeople = people.filter(person => person.heroId === heroId);
$.each(filteredPeople, 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");
});
}
答案 1 :(得分:1)
tbody
元素。$('#hero option:checked').val();
/* 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();
var hero = $('#hero option:checked').val();
//Populate table
$("#userdata tbody").empty()
$.each(people, function(index, person) {
if (hero == person.heroId) {
var tableRow =
"<tr>" +
"<td>" + person.firstName + "</td>" +
"<td>" + person.lastName + "</td>" +
"<td>" + person.job + "</td>" +
"<td>" + person.roll + "</td>" +
"</tr>"
$(tableRow).appendTo("#userdata tbody");
}
});
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="profile">
<select id="hero">
<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>
&#13;
答案 2 :(得分:1)
您可以从<select>
:
var heroid = $(".profile>select").val();
然后进行比较,使用return
退出.each
的当前迭代(相当于for循环中的continue
,而不是像a的完整退出break
)。
$.each(people, function(index, person) {
if (person.heroId != heroid) return;
/* 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().find("tbody").empty();
var heroid = $(".profile>select").val();
//Populate table
$.each(people, function(index, person) {
if (person.heroId != heroid) return;
var tableRow =
"<tr>" +
"<td>" + person.firstName + "</td>" +
"<td>" + person.lastName + "</td>" +
"<td>" + person.job + "</td>" +
"<td>" + person.roll + "</td>" +
"</tr>"
$(tableRow).appendTo("#userdata tbody");
});
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
&#13;