简体   繁体   English

jQuery追加一个元素

[英]jquery append an element after append

So I have an 2 objects, one contains supervisor and the other contains employee data and also the supervisor ID that corresponds to the employee(which will be used for the ul id). 因此,我有2个对象,一个包含主管,另一个包含员工数据以及与该员工相对应的主管ID(将用于ul id)。 I wanted them to look like this below, using JQuery append: 我希望他们使用JQuery append如下所示:

  • Supervisor A 主管A

    • Employee 1 员工1

    • Employee 2 员工2

  • Supervisor B 主管B

    • Employee 3 员工3

    • Employee 4 员工4

or in HTML Form: 或HTML形式:

<ul id="myEmployee">
   <li> Supervisor A
      <ul id="supervisorA">
          <li id="employee1">Employee 1</li>
          <li id="employee2">Employee 2</li>
      </ul>
   </li>
   <li> Supervisor B
      <ul id="supervisorB">
          <li id="employee3">Employee 3</li>
          <li id="employee4">Employee 4</li>
      </ul>
   </li>
</ul>

Initially I only have <ul id="myEmployee"></ul> to allow JQuery to append the elements. 最初,我只有<ul id="myEmployee"></ul>允许JQuery追加元素。

Sample Data (as requested): 样本数据(根据要求):

 supervisor = [{
     "SupervisorID": "SupervisorA",
     "DisplayName": "Supervisor A"
 }, {
     "SupervisorID": "SupervisorB",
     "DisplayName": "Supervisor B"
 }];

 employee = [{
     "SupervisorID": "SupervisorA",
     "employeeId": "employee1",
     "DisplayName": "Employee 1"
 }, {
     "SupervisorID": "SupervisorB",
     "employeeId": "employee2",
     "DisplayName": "Employee 2"
 }];

Using a for-loop, I first append the Supervisors first 使用for循环,我首先附加监督者

for (let x = 0; x < supervisor.length; x++) {
   $('#myEmployee').append('<li>' + supervisor[x].DisplayName + '<ul id=' + supervisor[x].SupervisorID + '></ul></li>');
}

Here comes the problem, when i try to append the employees into the ul that contains the id of the supervisors using another for loop 当我尝试使用另一个for循环将员工追加到包含主管ID的ul中时,问题就来了

for (let x = 0; x < employee.length; x++) {                
   $('#'+employee[x].SupervisorID).append('<li>' + employee[x].employeeID+'</li>');
}

Console told me that the JQuery could not find the ID of the ul that i have appended previously. 控制台告诉我,JQuery找不到我之前附加的ul的ID。 In other words, jQuery thinks that the ul id was not initialised. 换句话说,jQuery认为ul id尚未初始化。

Is there a way to fix this? 有没有办法解决这个问题?

You have to understand how JS arrays and objects work. 您必须了解JS数组和对象如何工作。 First understand that and you'll see what went wrong. 首先了解这一点,您会发现问题出在哪里。

Change the first loop to this: 将第一个循环更改为此:

for (let x = 0; x < supervisor.length; x++) {
   $('#myEmployee').append('<li>' + supervisor[x].DisplayName + '<ul id=' + supervisor[x].SupervisorID + '></ul></li>');
}

And second loop to this: 第二循环:

for (let x = 0; x < employee.length; x++) {                
   $('#'+employee[x].SupervisorID).append('<li id=' + employee[x].employeeID + '>' + employee[x].DisplayName+'</li>');
}

The sample data you gave shows 2 arrays of objects, not 2 objects. 您提供的样本数据显示了2个对象数组,而不是2个对象。 You are also not accessing the array indexes and object keys correctly as @Andreas pointed out above. 您也无法正确访问数组索引和对象键,如@Andreas所指出的那样。

Once you correct the code to access the arrays and objects within the jQuery should work. 一旦更正代码,即可访问jQuery中的数组和对象。

for (let x = 0; x < supervisor.length; x++) {
    $('#myEmployee').append('<li>' + supervisor[x].DisplayName + '<ul id=' + supervisor[x].SupervisorID + '></ul></li>');
}

for (let x = 0; x < employee.length; x++) {                
   $('#' + employee[x].SupervisorID ).append('<li>' + employee[x].DisplayName + '</li>');
}

Working example: https://jsfiddle.net/pjpwea/pLu5j3fh/2/ 工作示例: https : //jsfiddle.net/pjpwea/pLu5j3fh/2/

Jsfiddle You are accessing the objects in a wrong manner Jsfiddle您以错误的方式访问对象

First For Loop Fix : 首先进行循环修复:

 $('#myEmployee').append('<li>' + supervisor[x].DisplayName + '<ul id=' + supervisor[x].SupervisorID + '></ul></li>');

Second For loop Fix : Second For循环修复:

for (let x = 0; x < employee.length; x++) {                
   $('#'+employee[x].SupervisorID).append('<li>' + employee[x].employeeId+'</li>');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM