dtvnbe1428 2014-09-17 09:01
浏览 301
已采纳

JQuery foreach计数

I have a piece of code that fetches results from my DB and then shows it on my page via jQuery:

var searchItem = function() {
    $('#search_results').css('top', '0px');
    $.getJSON('/json/searchitem/q/' + $('#json_search').val(), function(data){
        $('#search_results ul').fadeIn().html('');

        if($('#search_results table').css('display')=='none'){
            $('#search_results table').fadeIn();
        }

        $.each(data, function(i,item){
            $('#search_results').append('<tr><td>'+item.code+'</td></tr>');
        });
    });
};

Now, what I want is when my results are > 10 I want to show it in a table with small rows, 5 to 10 results in a normal table result and < 2 in big divs.

Can anyone point me in the right direction on how to do this count? I'm not experienced in JQuery yet.

  • 写回答

2条回答 默认 最新

  • dsfjnxjlbqv9812 2014-09-17 09:09
    关注
    var searchItem = function() {
        $('#search_results').css('top', '0px');
        $.getJSON('/json/searchitem/q/' + $('#json_search').val(), function(data){
            $('#search_results ul').fadeIn().html('');
    
            if($('#search_results table').css('display')=='none'){
                $('#search_results table').fadeIn();
            }
           //Checks If result length is lessthan 2
            if(data.length < 2){
                // Show in big divs
            }else{
                //  show it in a table with small rows
                 $.each(data, function(i,item){
                   $('#search_results').append('<tr><td>'+item.code+'</td></tr>');
                 });
            }
        });
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器