我有一个工作代码,用于搜索json记录并逐个显示所有记录。我想根据搜索词显示记录
工作代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery PHP Json Response</title>
<style type="text/css">
div
{
text-align:center;
padding:10px;
}
#msg {
width: 500px;
margin: 0px auto;
}
.members {
width: 500px ;
background-color: beige;
}
</style>
</head>
<body>
<input type="text" id="search-json-input" />
<input type="button" id="search-json-submit" value="search" />
<br/>
<br/>
<input type="button" name="next" id="next" value="NEXT" />
<br/>
<input type="button" name="previous" id="previous" value="PREV" />
<br/>
<div id="msg">
<table id="userdata" border="1">
<thead>
<th>Email</th>
<th>Sex</th>
<th>Location</th>
<th>Picture</th>
<th>audio</th>
<th>video</th>
</thead>
<tbody></tbody>
</table>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript">
var users = [];
var idx = 0;
var renderRow = function (idx) {
if (idx < 0) idx = 0;
if (idx > (users.length - 1)) idx = (users.length - 1);
var user = users[idx];
var tblRow = "<tr>" + "<td>" + user.email + "</td>" + "<td>" + user.sex + "</td>" + "<td>" + user.location + "</td>" + "<td>" + "<img src=" + user.image + ">" + "</td>" + "<td>" + "<audio src=" + user.video + " controls>" + "</td>" + "<td>" + "<video src=" + user.video + " controls>" + "</td>" + "</tr>";
$('#userdata tbody').html(tblRow);
};
var url = "json.php";
$.getJSON(url, function (data) {
users = data.members;
renderRow(idx);
$('#next').click(function() {
idx++;
renderRow(idx);
});
$('#previous').click(function() {
idx--;
renderRow(idx);
});
});
</script>
</body>
</html>
json.php的结果可以在这里看到:http://sco7.com/components/phonegap/json.php
答案 0 :(得分:0)
创建的按钮有一个onclick处理程序:
重复直到有效更改索引
删除当前显示的项目文本,并将项目附加到更新的索引处。