无法查看特定信息.单击事件.Javascript

Not able to view specific information. Click events. Javascript

本文关键字:信息 单击 事件 Javascript      更新时间:2024-06-18

我已经收集了一份公司列表,可以点击这些列表来显示公司数据。总共有10家公司。问题是,当我单击一个链接查看特定公司的数据时,所有数据(所有10家公司的数据)都会出现。

这是我的问题图片。单击此处。如您所见,Topsome已被单击,但所有信息都已显示。

这是我正在使用的javascript代码。

此函数(findCompanies.js)收集公司列表。

function findCompanies()
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var companies = JSON.parse(xhttp.responseText);
      var list = "<p>There are totally "+companies.totalResults+" companies of which the 10 first are listed here.</p>";
      for (i=0;i<10;i++)
      {
      company = companies.results[i].name;
      list = list + "<a href='#/' onclick='findCompanyData(company);'>"+company + "</a><br/>";
      }
      document.getElementById("answersver").innerHTML = list;
    }
  }
  url = "http://avoindata.prh.fi:80/tr/v1?totalResults=true&maxResults=10&resultsFrom=0&companyRegistrationFrom="+document.input.start.value+"&companyRegistrationTo="+document.input.end.value;
  xhttp.open("GET", url, true);
  xhttp.send();
}

此功能提供公司的详细信息。

function findCompanyData(company)
{
  var xhttp2 = new XMLHttpRequest();
  xhttp2.onreadystatechange = function() {
    if (xhttp2.readyState == 4 && xhttp2.status == 200) {
      var companydata = JSON.parse(xhttp2.responseText);
      var list = "<h6>Company Information: </h6>";
      for (i=0;i<10;i++)
      {            
       var name = "<p>Company Name: "+companydata.results[i].name +" </p>";
       var companyId = "<p>Company ID#: "+companydata.results[i].businessId +" </p>"; 
       var registrationDate = "<p>Registration Date: "+companydata.results[i].registrationDate +" </p>";
       var companyType = "<p>Company Type: "+companydata.results[i].companyForm +" </p>";
       list = list + name + companyId + registrationDate + companyType;   
      }
     document.getElementById("companydata").innerHTML = list;
    }
  }
  url = "http://avoindata.prh.fi:80/tr/v1?totalResults=true&maxResults=10&resultsFrom=0&companyRegistrationFrom="+document.input.start.value+"&companyRegistrationTo="+document.input.end.value;
  xhttp2.open("GET", url, true);
  xhttp2.send();
}

有什么建议吗?提前感谢!

试试这个。

function findCompanies()
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var companies = JSON.parse(xhttp.responseText);
      var list = "<p>There are totally "+companies.totalResults+" companies of which the 10 first are listed here.</p>";
      for (i=0;i<10;i++)
      {
      company = companies.results[i].name;  // Without quotes, 'company' will be treated as a var
      company = '"'+company+'"';            // ADDED THIS LINE to quote the company
      list = list + "<a href='#/' onclick='findCompanyData("+company+");'>"+company + "</a><br/>";
      }
      document.getElementById("answersver").innerHTML = list;
    }
  }
  url = "http://avoindata.prh.fi:80/tr/v1?totalResults=true&maxResults=10&resultsFrom=0&companyRegistrationFrom="+document.input.start.value+"&companyRegistrationTo="+document.input.end.value;
  xhttp.open("GET", url, true);
  xhttp.send();
}

第二个功能

function findCompanyData(company)
{
  var xhttp2 = new XMLHttpRequest();
  xhttp2.onreadystatechange = function() {
    if (xhttp2.readyState == 4 && xhttp2.status == 200) {
      var companydata = JSON.parse(xhttp2.responseText);
      var list = "<h6>Company Information: </h6>";
      for (i=0;i<10;i++)
      {
          // Only add info to be shown if this was the company clicked.
          if (companydata.results[i].name === company) {    // ADDED THIS LINE
              var name = "<p>Company Name: "+companydata.results[i].name +" </p>";
              var companyId = "<p>Company ID#: "+companydata.results[i].businessId +" </p>"; 
              var registrationDate = "<p>Registration Date: "+companydata.results[i].registrationDate +" </p>";
              var companyType = "<p>Company Type: "+companydata.results[i].companyForm +" </p>";
              list = list + name + companyId + registrationDate + companyType;   
          }
      }
     document.getElementById("companydata").innerHTML = list;
    }
  }
  url = "http://avoindata.prh.fi:80/tr/v1?totalResults=true&maxResults=10&resultsFrom=0&companyRegistrationFrom="+document.input.start.value+"&companyRegistrationTo="+document.input.end.value;
  xhttp2.open("GET", url, true);
  xhttp2.send();
}