2016-07-04 52 views
0

我有一個HTML/CSS搜索欄,用戶可以在其中輸入關鍵字,點擊後,我的打開狀態JSON API代碼返回與該關鍵字匹配的新澤西狀態帳單。如何超鏈接帶有可變URL地址的JSON API返回值?

Search Bar Screenshot

Screenshot of a Result

我想要返回到超鏈接至其在新澤西州議會的網站頁面法案冠軍,但我只可以找到如何超鏈接有一個返回指令靜態網站。

這裏是我的JavaScript代碼到目前爲止(與API密鑰刪除):

e.preventDefault(); 
    // console.log($("#billID").val()); 
    var billSearchValue = $("#billID").val(); 
    if(billSearchValue=='') 
    { 
     alert("Enter Desired Query Parameters"); 
    } else{ 
     // console.log(billSearchValue); 
    } 
    var searchQuery = "&q=" + billSearchValue; 
    var baseUrl = "http://openstates.org/api/v1/bills/?state=nj"; 
    var apiKey = ""; 
    var apiKeyParam = "&apikey="; 
    var apiKeyParams = apiKeyParam + apiKey; 
    var urlJSON = baseUrl + searchQuery + apiKeyParam + apiKey; 
    // console.log(urlJSON); 
    $.getJSON(urlJSON, function (data) { 
     var billsVar = []; 
     $.each(data, function(key, val) { 
      billsVar.push(val); 
     }); 
     for (var i = 0; i < billsVar.length; i++) { 
      var billList = "<li>Bill <ul class=\"ul-sub\">" 
      var billTitle = "<li><strong>Bill Title</strong>: " + billsVar[i]['title'] + "</li>"; 
      var billCreatedAt = "<li><strong>Bill Created</strong>: " + billsVar[i]['created_at'] + "</li>"; 
      var billUpdatedAt = "<li><strong>Bill Updated</strong>: " + billsVar[i]['updated_at'] + "</li>"; 
      var billID = "<li><strong>ID</strong>: " + billsVar[i]['id'] + "</li>"; 
      var billChamber = "<li><strong>Bill Chamber</strong>: " + billsVar[i]['chamber'] + "</li>"; 
      var billState = "<li><strong>Bill State (Probably Don't Want/Need This)</strong>: " + billsVar[i]['state'] + "</li>"; 
      var billSession = "<li><strong>Bill Session</strong>: " + billsVar[i]['session'] + "</li>"; 
      var billType = "<li><strong>Bill Type</strong>: " + billsVar[i]['type'] + "</li>"; 
      var billSubjects = "<li><strong>Subjects</strong>: " + billsVar[i]['subjects'] + "</li>"; 
      var billBillID = "<li><strong>Bill ID</strong>: " + billsVar[i]['bill_id'] + "</li>"; 
      var billOutput = billList + billTitle + billCreatedAt + billUpdatedAt + billID + billChamber + billState + billSession + billType + billSubjects + billBillID + "</ul></li>"; 
      $("#jsonlist").append(billOutput); 
     } 
    }); 
}) 

});

回答

1

後有點研究,我看到一個超鏈接賬單是這樣的:

http://openstates.org/nj/bills/{Bill Session}/{Bill ID}/

我無法測試我的代碼,因爲我沒有API密鑰,但解決辦法是這樣的:

var billTitle = '<li><strong>Bill Title</strong>: ' 
    + '<a href="http://openstates.org/nj/bills/' + 
    billsVar[i]['session'] + '/' + billsVar[i]['bill_id'].split(' ').join('') + '/">' 
    + billsVar[i]['title'] + '</a></li>'; 
+0

謝謝!我試過了,現在我得到一個「Uncaught ReferenceError:標題沒有定義。」 – Karissa

+0

哦,對不起,因爲我正在用局部變量進行測試,所以留下了那個。現在修復。 – yuriy636

+0

對不起,但由於某種原因超鏈接都導致404錯誤。 – Karissa