admin管理员组文章数量:1434363
I am able to show Json
data in html
view, but how can I now filter
json data list based on firstname
as I type in search
input box, please help me I am new to 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);
});
Here is the fiddle.
I am able to show Json
data in html
view, but how can I now filter
json data list based on firstname
as I type in search
input box, please help me I am new to 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);
});
Here is the fiddle.
Share Improve this question edited Jun 19, 2015 at 9:38 Amit asked Jun 19, 2015 at 9:25 AmitAmit 85710 silver badges18 bronze badges2 Answers
Reset to default 4you can use following
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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="search" name="search" id="search" value="" />
<div id="placeholder"></div>
Up Dated
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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="search" name="search" id="search" value="" />
<div id="placeholder"></div>
Up Dated 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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="search" name="search" id="search" value="" />
<div id="placeholder"></div>
use $.grep
See this example: http://jsfiddle/kevalbhatt18/ejPV4/317/
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
}
}
]}
var found_names = $.grep(data.users, function(v) {
return v.firstName === "John" ;
});
console.log(found_names);
Edit
see this fiddle with key press event.
http://jsfiddle/kevalbhatt18/ejPV4/320/
本文标签: javascriptjQuery Json filter with search input without using any filter pluginStack Overflow
版权声明:本文标题:javascript - jQuery Json filter with search input without using any filter plugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745628168a2667101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论