我正在尝试使用NYT Article Search API更改页面变量,所以我可以转到下10个结果。我已经将页码作为变量,我只需要使用另一个函数对其进行更改,或者在单击下一页或上一页的链接时重新加载查询即可。
function articleSearch() {
$('nav').hide();
var pageCount = 0;
var search = $('#search').val();
$('#articles').submit(function(evt) {
$('#searchHead').remove();
evt.preventDefault();
// var search = $('#search').val();
var fromAlt = $('#fromAlt').val();
var toAlt = $('#toAlt').val();
var sort = $('#sort').val();
var formData = $('#articles').serialize();
formData = encodeURI(formData);
var url = "http://api.nytimes.com/svc/search/v2/articlesearch.json";
url += '?' + $.param({
'api-key': "API-KEY-GOES-HERE",
'q': '"' + search + '"',
'begin_date': fromAlt,
'end_date': toAlt,
'sort': sort,
'fl': "headline, byline, pub_date, snippet, web_url",
'page': pageCount
});
$.getJSON(url, displayResults);
}); // end article submit func
}
function displayResults(dataFromServer) {
$('#searchHead').remove();
$('#resultsList').remove();
$('#articleHeader').remove();
var results = dataFromServer.response.docs;
var metaTag = dataFromServer.response.meta;
if (metaTag.hits < 10) {
$('nav').hide();
} else {
$('nav').show();
}
$('#articles').after('<h2 id="searchHead"></h2>');
$('nav').after('<ol id="resultsList"></ol>');
$('nav').after('<p id="articleHeader">Viewing articles ' + ' of ' + metaTag.hits);
var fromDate = $('#fromDate').val();
var toDate = $('#toDate').val();
var searchTerm = $('#search').val();
var searchString = "You searched for " + searchTerm + " from " + fromDate + " to " + toDate;
$('nav').append('<h2 id="searchHead">' + searchString + '</h2>');
$.each(results, function(resultsIndex, resultsValue) {
var headline = resultsValue.headline.main;
var byline = resultsValue.byline.original;
var pubDate = resultsValue.pub_date.slice(0, 10);
var snippet = resultsValue.snippet;
var link = resultsValue.web_url;
var article = "<a href='" + link + "'>" + headline + "</a> " + byline + '<br>' + pubDate + "<br>" + snippet;
var li = '<li>' + article + '</li>';
$('#resultsList').append(li);
}); //end each func
}