2
How to search for data in a JSON with AJAX ? I want to search a JSON field named tag and show another field (title and url) in a div.
HTML:
<div class="Minimizado top-menu-invisible hidden-mobile hidden-md hidden-sm">
<input id="searchterm" name="searchterm" />
<button id="search">Buscar</button>
<div id="results">
</div>
</div>
JS:
<script>
$("#searchterm").keyup(function(e) {
var q = $("#searchterm").val();
$.getJSON("userControls/HelpPaginas.json",
function(data){
$("#results").empty();
$.each(data, function(i, item) {
$("#results").append("<div>" + item.title + "<br><br></div>");
});
}
);
});
</script>
JSON:
[{
"Tag": "Toyota, Ford, Carro2, teste",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "Ford, chevy, opala, teste2",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "BMW, Audi, importado",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "Benz, Teste3, cars",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "GMC, Chevrolet, chevy, GM",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "HUMMER, 4x4, teste4, agora vai, teste com tag",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}]
But the worst error happens! not the error in the browser, just do not search. It even makes the correct call in JSON but does not display in DIV
New: I modified the JS and brought the data, but not yet search, just returns all the data, how to search the field tag?
New JS:
$.getJSON("../userControls/HelpPaginas.json", function (data) {
var html = [];
/* loop through array */
$.each(data, function (index, d) {
html.push("title: ", d.title, ", ",
"tag: ", d.Tag, ", ",
"url: ", d.Url, "<br>");
});
$("#results").html(html.join('')).css("background-color", "orange");
});
Debugging in the browsed your return in
data
is thisJSON
up?– Guilherme Lautert
Do tests using
$(document).on('change blur', "#searchterm", function(e){ /* ... */ });
– KaduAmaral
Want to filter the data based on the field
tag
?– KaduAmaral
yes...but I’m getting it now with if (d.Tag.toLowerCase.search(Fieldsearch) != -1) {
– Dorathoto
How are you sending the data to the server? I don’t see where on this
$.getJSON
you’re using theq
.– Sergio