我不知道如何讓它工作,以便它從getrandomvideos中獲取變量並在jquery語句中使用它。我希望用戶按下一個按鈕,然後隨機選擇一個名稱,然後提交給jquery語句,然後格式化返回的JSON。在jQuery中使用函數的變量
<!doctype html>
<html>
<head>
<title>Trove Basic Search</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<h2>Search Trove</h2>
<button type="submit" id="searchbtn">Search</button>
<hr />
<div id="output">
<h4>Search Results</h4>
</div>
<script type="text/javascript">
function getRandomVideo() {
var searches = [
'beethoven',
'mozart',
'beethoven',
'debussy',
]
var rand = Math.floor(Math.random()*videos.length);
var search = searches[rand];
return search
}
var apiKey = "jja10ssv4950uh65";
var searchTerm = getRandomVideo();
$(document).ready(function(){
// There is an issue with TROVE applications where the first search will result in nothing being returned
// To get around this, we perform a dummy submit. Not sure how to do this
$("#searchbtn").submit
$("#searchbtn").submit(function() {
var url = "http://api.trove.nla.gov.au/result?key=" + apiKey + "&encoding=json&zone=newspaper&sortby=relevance&q=" + searchTerm + "&s=0&n=5&include=articletext,pdf&encoding=json&callback=?";
console.log(url);
$.getJSON(url, function(data) {
$('#output').empty();
$.each(data.response.zone[0].records.article, function(index, value) {
$("#output").append("<p>" + value.articleText +"</p>");
});
});
});
});
</script>
</body>
</html>
當前代碼發生了什麼? –
我已經複製了從以前的網站,將有一個提交的表格,然後找到JSON的jQuery –