首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >javascript循环通过数组显示详细信息-循环通过第二个数组显示第二个url的详细信息

javascript循环通过数组显示详细信息-循环通过第二个数组显示第二个url的详细信息
EN

Stack Overflow用户
提问于 2018-07-08 20:34:10
回答 2查看 102关注 0票数 0

我希望建立一个应用程序,搜索一个人的名字-我已经实现了这一部分-并显示人名。应用程序的第二部分也应该呈现每个人点击更多的详细信息(此信息应从第二个URL检索。最后,当我点击那个人时,应用程序也应该注入一个图像。我一直没能把这事办好。你们能不能帮我修一下或者开个灯?

我已经注释掉了我一直在尝试做的事情。

https://codepen.io/edmonteiro/pen/qKeMLj

代码语言:javascript
复制
document.getElementById("subBtn").onclick = function(event){
//prevent default submission
event.preventDefault();

//grab the typed value in the text box
var name = document.getElementsByClassName("name")[0].value;

//Alternatively give an "id" attribute to the text box in the HTML
//for example, <input type="text" name="textboxname" class="name">, use:
//var name = document.getElementById("textboxname").value();

let ourRequest = new XMLHttpRequest();

//pass this search value as a parameter in URL to get results from database
ourRequest.open('GET', 'https://izrite.com:5555/leads?page=1&name=' + name, true);

//function to call when the AJAX request is completed
ourRequest.onload = function(){
//if request completed successfully
if(ourRequest.status === 200){
  let ourData = JSON.parse(ourRequest.responseText);

  let list = document.getElementById("list");

  //process only if request returned a valid JSON data
  if(typeof ourData === 'object'){
    //if there are 1 or more results found
    if(ourData.length > 0){
      list.innerHTML = "<p><b>Matched names from database:</b></p>";

      //for each `lead` in the array, print its name
      for(lead of ourData){
        list.innerHTML += "<p>Name: " + lead.name + "</p>";
      /*-----------------------------------------------------------------*/
      /* Second and third part of the test - to be continued. Not finished before the 5hours time frame mentioned on the challenge specs*/
        // if((lead.id).value()==="0009bd06-a9ce-470e-b13a-acd2aaaa42d4"){
        //   let ourRequest2 = new XMLHttpRequest();
        //   ourRequest2.open('GET', 'https://izrite.com:5555/lead/0009bd06-a9ce-470e-b13a-acd2aaaa42d4', true);

        //   let moreDetails = document.getElementById('moreDetails');
        //   let ourData2 = JSON.parse(ourRequest2.responseText);
        //   if(typeof ourData2 === 'object'){
        //     //if there are 1 or more results found
        //     if(ourData2.length > 0){
        //       moreDetails.innerHTML = "<p><b>More details</b></p>";

        //       //for each `lead` in the array, print its name
        //       for(detl of ourData2){
        //         moreDetails.innerHtml += "<p>Name: " + detl.name + "</p><br><p>Make: " + detl.make + "</p><br><p>Model: " + detl.model + "</p><br><p>Derivative: " + detl.derivative + "</p>";

        //         // var myImage = new Image(100, 200);
        //         // myImage.src = 'https://izrite.com:5555/image/71890';
        //         // document.body.appendChild(myImage);
        //       }
        //     }
        //   }
        // }
        // ourRequest2.send();
      /*-----------------------------------------------------------------*/

      };
    }else{
      //no results found for this search query
      list.innerHTML = "<p><b>No match found in database!</b></p>";
    }
  }else{
    //response not in JSON format
    list.innerHTML = "<p><b>Response is not in valid JSON format!</b></p>";
  }
 }
}

 //send the AJAX request
 ourRequest.send();
}
EN

回答 2

Stack Overflow用户

发布于 2018-07-08 20:41:07

forEach回调将当前值作为第一个参数,将索引作为第二个参数。尝试:

代码语言:javascript
复制
ourData.forEach((element, index) => {
  document.getElementById("app").innerHTML = `<p>${element.name}</p>`;
});

票数 0
EN

Stack Overflow用户

发布于 2018-07-08 21:43:06

试试这个ourData.forEach((element,=> )=>{ document.getElementById("app").innerHTML += <p>${element.name}</p>;});innerHTML +=

在每个循环中添加到HTML。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51232046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档