【发布时间】:2016-09-06 07:24:27
【问题描述】:
我对 java-script 没有太多经验,而且我对 java-script 回调有困难。我想知道这里是否有专家可以帮助我摆脱与此代码的斗争。 我的想法是我正在尝试从 .json 文件回调并通过 id 搜索它。 我可以看到所有客户,但无法搜索其中任何一个。
这是我的代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Kunde</title>
</head>
<body>
<div>
<h2>Kunde</h2>
<ul id="ALLEKUNDE" />
</div>
<div>
<h2>Search by ID</h2>
<input type="text" id="KUNDE" size="5" />
<input type="button" value="Search" onclick="find();" />
<p id="RESULTS" />
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
var uri = 'json.json';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(url)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#ALLEKUNDE'));
});
});
});
function formatItem(item) {
//return item.name + item.phone;
return item.ID + item.name + item.phone + item.contact + item.balanceLCY + item.creditLimitLCY;
//'ID= ' + item.ID, + 'name = ' + item.name, "<br />" + 'phone = ' + item.phone, "<br />" + 'contact = ' + item.contact, "<br />" + 'balanceLCY= ' + item.balanceLCY, "<br />" + 'creditLimitLCY = ' + item.creditLimitLCY
}
function find() {
var id = $('#KUNDE').val();
$.getJSON(uri)
.done(function (data) {
$('#RESULTS').text(formatItem(data));
})
.fail(function (jqXHR, textStatus, err) {
$('#RESULTS').text('Error: ' + err);
});
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript jquery json getjson