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

无法查看特定信息。单击事件。Javascript,javascript,ajax,Javascript,Ajax,我已经收集了一份公司列表,可以点击这些列表来显示公司数据。总共有10家公司。问题是,当我单击一个链接查看该特定公司的数据时,所有数据(所有10家公司)都会出现 这是我的问题的图片。如您所见,单击了Topsome,但显示了所有信息 下面是我正在使用的javascript代码 此函数(findCompanies.js)收集公司列表 function findCompanies() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatech

我已经收集了一份公司列表,可以点击这些列表来显示公司数据。总共有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("ansver").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();
}
函数查找公司()
{
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
var companys=JSON.parse(xhttp.responseText);
var list=“共有“+家公司。totalResults+”家公司,其中前10家在此处列出。

”; 对于(i=0;i试试这个

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("ansver").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();
}
函数查找公司()
{
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
var companys=JSON.parse(xhttp.responseText);
var list=“共有“+家公司。totalResults+”家公司,其中前10家在此处列出。

”;
对于(i=0;iReplace list=list+“
”,使用此list=list+“
”,再次感谢您的帮助。我按照您的建议做了,但仍然没有结果。我检查并看到错误“意外令牌”。然后它列出了此行
(函数(事件){findCompanies();})
查看您发布的代码,除非我仔细研究,否则我看不出有什么不同。请解释错误,以及在哪里和如何修复错误。向上投票(工具提示)显示“此答案有用”-代码转储不“有用”;找到此问答的人应该从中学习一些东西。当然,Stephen,你是对的。在第一种方法中,我尝试将公司名称字符串传递给方法参数。在第二种方法中,我过滤数据并仅选择包含公司名称的数据。我修复了代码。我希望它能工作!谢谢你的帮助!它能工作!我不知道它为什么会有不同的行为n哦,太棒了!谢谢你的帮助!我真的很感激!!:D
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();
}