This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
JSON array获取第一行结果后,我需要使用jquery每个方法打印所有结果。

这是我的语法

 $(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,使行像在数据库中一样打印?

更新

在对函数进行了一些调整之后,我添加了新功能以使用列表显示结果。

这是语法

$(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);
    });


问题是,如果要基于另一个关键字显示,我如何从中重置结果,因此如果用户键入新关键字,列表将很清楚?无需刷新浏览器。

谢谢。

更新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);
    });

最佳答案

在每个中,您可以捕获当前迭代中的元素:

$.each(data.content, function(i, val) {
    $('.toprightsec')
        .append("Title" + val.title)
        .append("Intro" + val.intro_text);
});

09-30 16:20
查看更多