displayItem関数を使用して配列jsonから次の項目を表示しようとしていますが、ボタンをクリックすると、フォームは最初の値(arrayResponse [0])を再ロードします。代わりに次のアイテムを表示するために何をすべきですか?JQueryは配列の次の項目を表示します
$(document).ready(function() {
var firstElement;
var arrayResponse;
var index = 0;
var request = new XMLHttpRequest();
// When the file has loaded,
request.onload = function() {
// parse the JSON text into an array of post objects.
arrayResponse = JSON.parse(request.responseText);
var firstelement = arrayResponse[0];
$("#name").val(firstelement.name);
$("#author").val(firstelement.author);
$("#content").val(firstelement.content);
// Pass the posts array to the callback.
};
request.open("GET", "http://127.0.0.1:8887/posts.json", true);
request.send(null);
$("#nextButton").bind("click", function() {
displayItem(arrayResponse[++index])
});
function displayItem(item) {
$("#name").val(item.name);
$("#author").val(item.author);
$("#content").val(item.content);
}
});
[フォームのHTMLに次の項目を表示する方法]の可能な重複(https://stackoverflow.com/questions/44761891/how-to -display-next-item-on-form-html) –
あなたは同じ質問を2回尋ねました:https://stackoverflow.com/questions/44761891/how-to-display-next-item-on-form-html、私これは良い方法だとは思わないStackOverflow –