追加数据无法在json中过滤? [英] append data can't filter in json?
本文介绍了追加数据无法在json中过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用jquery
和json
如何最好地过滤数据,但是我的代码无法在json中找到数组?
how do the best filtering data, but my code not find array in json ?
帮助查找解决了我的问题.谢谢
help find solved my problem. thanks
$(document).ready(function(){
$("#search").keyup(function(){
$("#result").html('');
var searchField = $("#search").val();
var expression = new RegExp(searchField, "i");
$.getJSON("https://raw.githubusercontent.com/marshallt09/taruhan88/master/kotakabupaten.json", function(result){
var x = result.listkotakabupaten;
for(var i = 0; i < x.length; i++){
if ( x[i].kotakab.search(expression) != 1 && x[i].kotakab.length != -1) {
$("#result").append("<li>"+x[i].kotakab+"</li>");
}
}
});
});
});
*{
margin: 0;
padding: 0;
}
ul {
width: 60%;
height: 100%;
}
li {
padding: 5px;
list-style: none;
border:1px solid;box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input type="text" name="search" id="search" placeholder="search">
<ul id="result"></ul>
这是我的jsfiddle代码此处
this my jsfiddle code here
推荐答案
x[i].kotakab.search(expression) != 1
不正确.如果未找到,则搜索返回-1.更改为:
x[i].kotakab.search(expression) != 1
is inocrect. A search returns -1 if not found. Change it to:
x[i].kotakab.search(expression) !== -1
https://jsfiddle.net/vgrj1L80/124/
这篇关于追加数据无法在json中过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文