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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献