jquery不根据搜索输入从json中获取结果
jquery not fetching results from json based on search input?
我有显示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 url = "json.php";
var search_query = jQuery('#search-json-input').val();
var search_query_regex = new RegExp(".*"+search_query+".*", "g");
$.getJSON(url, function (data) {
users = data.members;
renderRow(idx);
$('#next').click(function() {
idx++;
renderRow(idx);
});
$('#previous').click(function() {
idx--;
renderRow(idx);
});
});
$("#search-json-submit").click(function(){
for(var y=0;y<users.length;y++){
if((users[y].email).match(search_query_regex) ||
(users[y].sex).match(search_query_regex) ||
(users[y].location).match(search_query_regex)) {
renderRow(y)
}
}
});
var renderRow = function (idx) {
//alert(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);
};
</script>
</body>
</html>
json.php的结果可以在这里看到:http://sco7.com/components/phonegap/json.php
移动代码以将JSON获取到事件处理程序中。您的代码在出现时直接执行,因此在输入框中找不到任何值。
$("#search-json-submit").click(function(){
var url = "json.php";
var search_query = jQuery('#search-json-input').val();
var search_query_regex = new RegExp(".*"+search_query+".*", "g");
$.getJSON(url, function (data) {
users = data.members;
renderRow(idx);
$('#next').click(function() {
idx++;
renderRow(idx);
});
$('#previous').click(function() {
idx--;
renderRow(idx);
});
});
//... Rest of the handler
});
相关文章:
- JSON获取用户id's在嵌套数组中
- JSON:获取映射数据
- Jquery从JSON获取数据(未定义)
- 从 JSON 获取键值对而不使用 .(DOT) 在 Java 脚本或 jquery 中
- Django 使用 JSON 获取模型方法的值
- TypeError: 使用 Jquery JSON 获取数据时无效的 'in' 操作数 obj
- JSON获取地理位置在HTML中未返回值
- 从javascript中的JSON获取位置
- Angular Js无法使用浮点形式json获取int
- AngularJS,从json获取单个帖子
- 在服务器端创建html元素VS将数据作为JSON获取,并使用javascript创建标签
- 无效的“in”操作数 obj JQUERY - 尝试从 JSON 获取数据时出现类型错误
- 无法从 JSON 获取值
- 如何将动态数据从JSON获取到jvqmaps中,作为对象而不是来自php mysqli的数组
- 在 react.js 中从 Json 获取数据
- 从特定索引开始从 json 获取字符串值
- 如何在变量中存储从 JSON 获取的数据
- 无法从 JSON 获取数据
- 如何将 API 请求中的 JSON 获取到页面的 javascript 中
- 网格堆栈.js从 JSON 获取位置