我的代码应该显示来自API的结果但不会显示任何帮助。
$("#search").keypress(function(e){
if(e.keyCode == 13 || e.keyCode == 10){
var searchTerm1 = $("#search").val();
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ searchTerm1 +"&format=json&callback=?";
$.ajax({
url: url,
type: "GET",
async: false,
dataType: "json",
success: function(data, status, jqXHR){
// console.log(data);
for(var i = 0; i < data[1].length; i++) {
$("#output").prepend("<div><div class='well'><a href="+data[3][i]+"><h2>" + data[1][i]+ "</h2>" + "<p>" + data[2][i] + "</p></a></div></div>");
}
}
});
}
})
和HTML代码:
<form id="demo-2">
<input type="search" placeholder="Search" id="search">
</form>