0
index.htmlをjQueryのAJAX - 戻ると表示複数の結果
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="scripts.js"></script>
</head>
<body>
<h1>Order Request Form</h1>
<form id="create-form">
<table>
<tr>
<td>ID</td>
<td><input type="text" id="txtID"></td>
<td><button id="getid-button">GET Order by ID</button></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" id="txtName"></td>
<td><button id="getname-button">GET Order by Name</button></td>
</tr>
</table>
</form>
<div id="resultDiv"></div>
</body>
</html>
scripts.js
$(document).ready(function() {
var resultElement = $('resultDiv');
$('#getname-button').on('click',function(e) {
var searchName = $('#txtName').val();
e.preventDefault();
$.ajax ({
url: 'https://localhost:8080',
contentType: 'application/json',
}).then(function(response) {
for (var i = 0; i < response.length, i++) {
if (response[i].names.indexOf(searchName) > -1) {
resultElement.html('ID: ' + response[i].id + '</br>
+ 'Name: ' + response[i].name + '</br>');
}
}
});
});
});
だから私は何をしたいのかである私は、「GET注文を押すたび「名前で」ボタンをクリックして、結果をすべて表示してその中に名前を表示したい。今のところ、JSON配列を通過し、見つかった最新の結果を印刷しています。どのように私の問題を解決するために行くかもしれない任意のアイデア?前もって感謝します。
使用[ '.append()'](https://api.jquery.com/append/)の代わりに[ 'の.html()'](https://api.jquery.com/ html /) – Andreas
^^^ + '$( '#resultDiv')' – Satpal
'searchName'はどこに定義されていますか? – mplungjan