我从另一篇文章中借用了一些代码,所以我意识到col1指定对我的例子来说不是必需的,但基本上这只需要在选择某个主题(工资单,税收等)时填充某些员工信息。 。):
HTML:
<table class="table table-bordered table-striped">
<tr>
<th>
<select class="col1 selectTopic">
<option>Payroll</option>
<option>Tax</option>
<option>Accounts Payable</option>
</select>
</th>
</tr>
<tr>
<td class="col1 name"></td>
</tr>
<tr>
<td class="col1 photo"></td>
</tr>
<tr>
<td class="col1 email"></td>
</tr>
<tr>
<td class="col1 phone"></td>
</tr>
</table>
使用Javascript:
var data = {
"contacts":
{
"contact": [
{
"name": "Payroll",
"photo": "Emp 1 Photo",
"email": "emp1@mysite.com",
"phone": "4113834848"},
{
"name": "Tax",
"photo": "Emp 2 Photo",
"email": "emp2@mysite.com",
"phone": "4113834848"},
{
"name": "Accounts Payable",
"photo": "Emp 3 Photo",
"email": "emp3@mysite.com",
"phone": "4113834848"},
]}
}
$(".selectTopic").change(function() {
var jthis = $(this);
var whichCol;
if (jthis.hasClass("col1")) {
whichCol = "col1";
}
$.each(data.topics.topic, function(i, v) {
if (v.name == jthis.val()) {
$("td." + whichCol + ".name").html(v.name);
$("td." + whichCol + ".photo").html(v.photo);
$("td." + whichCol + ".email").html(v.email);
$("td." + whichCol + ".phone").html(v.phone);
return;
}
});
});
答案 0 :(得分:1)
您有data.topics.topic
,我相信您需要data.contacts.contact
https://jsfiddle.net/qfy397jt/
您知道如何在浏览器中使用开发人员工具/控制台吗?它会向您显示此错误的原因(data.topics
未定义)。 https://developers.google.com/web/tools/chrome-devtools/iterate/inspect-styles/shortcuts?hl=en如果您还没有使用Dev。之前的工具!