我想做的是,我有一个输入字段。在输入字段中输入内容后,如果向输出 json 的 php 文件发送请求,我想要的是根据搜索字段中的输入从 json 中获取结果。
索引.html
<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="divuseriemail"></div>
<div id="divusersex"></div>
<div id="divuserlocation"></div>
<div id="divuserimage"></div>
<div id="divuseraudio"></div>
<div id="divuservideo"></div>
<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++){
//console.log(users[y]);
if((users[y].email).match(search_query_regex) ||
(users[y].sex).match(search_query_regex) ||
(users[y].location).match(search_query_regex))
{
//console.log(users[y].email);
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 email = user.email;
$('#divuseremail').html(email);
var sex = user.sex;
$('#divusersex').html(sex);
var location = user.location;
$('#divuserlocation').html(location);
var image = "<img src=" + user.image + ">";
$('#divuserimage').html(image);
var audio = "<audio src=" + user.video + " controls>";
$('#divuseraudio').html(audio);
var video = "<video src=" + user.video + " controls>";
$('#divuservideo').html(video);
};
</script>