[英]jQuery Json filter with search input without using any filter plugin
我可以在html
视图中显示Json
数据,但是当我在search
输入框中键入内容时,现在如何基于firstname
filter
json数据列表,请帮助我成为javascript
& jQuery
新手。
HTML
<input type="search" name="search" id="search" value="" />
<div id="placeholder"></div>
使用Javascript / jQuery的
var data={"users":[
{
"firstName":"Ray",
"lastName":"Villalobos",
"joined": {
"month":"January",
"day":12,
"year":2012
}
},
{
"firstName":"John",
"lastName":"Jones",
"joined": {
"month":"April",
"day":28,
"year":2010
}
}
]}
$(data.users).each(function() {
var output = "<ul><li>" + this.firstName + " " + this.lastName + "--" + this.joined.month+"</li></ul>";
$('#placeholder').append(output);
});
这是小提琴 。
您可以使用以下
var data = { "users": [{ "firstName": "Ray", "lastName": "Villalobos", "joined": { "month": "January", "day": 12, "year": 2012 } }, { "firstName": "John", "lastName": "Jones", "joined": { "month": "April", "day": 28, "year": 2010 } }] } $(data.users).each(function () { var output = "<ul><li>" + this.firstName + " " + this.lastName + "--" + this.joined.month + "</li></ul>"; $('#placeholder').append(output); }); $('#search').change(function () { var yourtext = $(this).val(); if (yourtext.length > 0) { $("li:contains(" + yourtext + ")").addClass('notify'); } else{ $("li:contains(" + yourtext + ")").removeClass('notify'); } });
.notify{ border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="search" name="search" id="search" value="" /> <div id="placeholder"></div>
更新
var data = { "users": [{ "firstName": "Ray", "lastName": "Villalobos", "joined": { "month": "January", "day": 12, "year": 2012 } }, { "firstName": "John", "lastName": "Jones", "joined": { "month": "April", "day": 28, "year": 2010 } }] } $(data.users).each(function () { var output = "<ul><li>" + this.firstName + " " + this.lastName + "--" + this.joined.month + "</li></ul>"; $('#placeholder').append(output); }); $('#search').keyup(function () { var yourtext = $(this).val(); if (yourtext.length > 0) { $("li:not(:contains(" + yourtext + "))").hide(); } else{ $("li").show(); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="search" name="search" id="search" value="" /> <div id="placeholder"></div>
过期2
var data = { "users": [{ "firstName": "Ray", "lastName": "Villalobos", "joined": { "month": "January", "day": 12, "year": 2012 } }, { "firstName": "John", "lastName": "Jones", "joined": { "month": "April", "day": 28, "year": 2010 } }] } $(data.users).each(function () { var output = "<ul><li>" + this.firstName + " " + this.lastName + "--" + this.joined.month + "</li></ul>"; $('#placeholder').append(output); }); $('#search').keyup(function () { var yourtext = $(this).val(); if (yourtext.length > 0) { var abc = $("li").filter(function () { var str = $(this).text(); var re = new RegExp(yourtext, "i"); var result = re.test(str); if (!result) { return $(this); } }).hide(); } else { $("li").show(); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="search" name="search" id="search" value="" /> <div id="placeholder"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.