从JSON数组获取列表结果 [英] Getting List Result From JSON Array
问题描述
从JSON array
获取第一行结果后,我需要使用jquery
每种方法打印所有结果.
After getting the first row result from JSON array
, I need to print the all result using jquery
each method.
这是我的语法
$(document).ready(function () {
$("#btnsearch").click(function() {
valobj = $('#search_box').val();
$.getJSON("search.php", { q : valobj }, function(data,result){
//show result from database
$.each(data.content, function() {
$('.toprightsec').append("Title" + data.content[0].title)
.append("Intro" + data.content[0].intro_text);
});
//end show result
}, JSON);
});
data.content[0]
仅显示带有循环的第一行.但是数据本身并没有改变.如何解决data.content[0]
.title,使行像在数据库中一样打印?
The data.content[0]
is showing the first row only with looping. But the data it self not changed. How to solve the data.content[0]
.title, so the row is printed like in database?
更新
在对函数进行了一些调整之后,我添加了新功能以使用列表显示结果.
After creating some tweak to my function, i have added new feature to show the result using list.
这是语法
$(document).ready(function () {
//function to get the result from user-input
$("#btnsearch").click(function() {
valobj = $('#search_box').val();
$.getJSON("search.php", { q : valobj }, function(data,result){
//show result from database
$.each(data.content, function(index, value) {
$("#divContent").show();
var li = $("<li><a></a><br /><p></p></li>");
$("#posting").append(li);
$("a",li).text(value.title);
$("p",li).text(value.intro_text);
});
//end show result
}, JSON);
});
问题是,如果要基于另一个关键字显示,我如何才能从中重置结果,因此如果用户键入新关键字,该列表将很清楚?无需刷新浏览器.
The question is, how i can reset the result from if, we want to show based on another keyword, so the list is clear if user type new keywords? without refreshing the browser.
谢谢.
更新2
$(document).ready(function () {
//function to get the result from user-input
$("#btnsearch").click(function() {
//clear the div
$("#divContent").html("");
valobj = $('#search_box').val();
$.getJSON("search.php", { q : valobj }, function(data,result){
//show result from database
$.each(data.content, function(index, value) {
$("#divContent").show();
var li = $("<li><a></a><br /><p></p></li>");
$("#posting").append(li);
$("a",li).text(value.title);
$("p",li).text(value.intro_text);
});
//end show result
}, JSON);
});
推荐答案
在每种方法中,您都可以捕获当前迭代中的元素:
In the each you can capture the element at the current iteration:
$.each(data.content, function(i, val) {
$('.toprightsec')
.append("Title" + val.title)
.append("Intro" + val.intro_text);
});
这篇关于从JSON数组获取列表结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!