我有這個令人敬畏的工作代碼,從維基百科拉動自動填充建議。這非常有用,除了我希望它只推薦樂隊。舉例來說,輸入「Pink」只會返回以「Pink」開頭的/ bands /,而不是世界上處理「Pink」的所有內容。如何將Wikipedia API自動填充建議限制到某個類別?
有誰知道如何做到這一點? http://codepen.io/anon/pen/VePapK
我發現有一種方法,通過查詢動作here(而不是OpenSearch的)來過濾按類別的結果,但奇怪的是,似乎沒有成爲一個廣泛的「音樂」或「帶」類別。所以這是開放更多未回答的問題....任何幫助表示讚賞。
編輯:另外,如果有人知道一個更簡單的方法來更新列表中的所有重要樂隊在您的網站的下拉列表,請讓我知道。
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="get" id="search">
<input type="text" class="searchbox" value="Type here.. " onblur="if(this.value == '') { this.value = 'Type here..'; }" onfocus="if(this.value == 'Type here..') { this.value = ''; }" name="s">
<button type="submit">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
$(".searchbox").autocomplete({
source: function(request, response) {
console.log(request.term);
$.ajax({
url: "http://en.wikipedia.org/w/api.php",
dataType: "jsonp",
data: {
'action': "opensearch",
'format': "json",
'search': request.term
},
success: function(data) {
response(data[1]);
}
});
}
});
</script>
</body>
</html>