gpt4 book ai didi

Javascript:基于下拉列表填充表

转载 作者:行者123 更新时间:2023-12-03 07:11:30 25 4
gpt4 key购买 nike

我从另一篇文章中借用了一些代码,所以我意识到 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;
}
});

});

最佳答案

您有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。之前的工具!

关于Javascript:基于下拉列表填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36601890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com