我很难理解如何从 html 搜索框获取搜索并将其输入到 JS 端的 api 链接中。我想要这样的东西工作
$.getJSON(`https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=$(".stock-name")&apikey=APIKEY`,
function(data){
console.log(data);
var date = data["Meta Data"]["3. Last Refreshed"];
var stock = data["Meta Data"]["2. Symbol"];
var info = data["Meta Data"]["1. Information"];
var open = data["Time Series (Daily)"][date]["1. open"];
var close = data["Time Series (Daily)"][date]["4. close"];
var high = data["Time Series (Daily)"][date]["2. high"];
var low = data["Time Series (Daily)"][date]["3. low"];
var vol = data["Time Series (Daily)"][date]["5. volume"];
var difference = close-open;
$(".Stock-Name").append(stock);
$(".info").append(info);
$(".stock-open").append(open);
$(".stock-close").append(close);
$(".difference").append(difference);
$(".date").append(date);
$(".high").append(high);
$(".low").append(low);
$(".vol").append(vol);
if(difference < 0){
$(".stock").css("background-color", "red");
}
else{
$(".stock").css("background-color", "green");
}
}
);
其中 url 中的 symbol=$(".stock-name") 是用户在前端输入的股票名称,它将显示 api 中数组的信息。
回首忆惘然
相关分类