Javascript jqueryforeach计数

Javascript jqueryforeach计数,javascript,php,jquery,count,Javascript,Php,Jquery,Count,我有一段代码,从数据库中获取结果,然后通过jQuery将其显示在我的页面上: var searchItem = function() { $('#search_results').css('top', '0px'); $.getJSON('/json/searchitem/q/' + $('#json_search').val(), function(data){ $('#search_results ul').fadeIn().html('');

我有一段代码,从数据库中获取结果,然后通过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>');
        });
    });
};
var searchItem=function(){
$(“#搜索结果”).css('top','0px');
$.getJSON('/json/searchitem/q/'+$('#json_search')).val(),函数(数据){
$('#搜索结果ul').fadeIn().html('');
if($('search#u results table').css('display')=='none'){
$(“#搜索结果表”).fadeIn();
}
$。每个(数据、功能(i、项){
$(“#搜索结果”).append(“”+item.code+“”);
});
});
};
现在,我想要的是当我的结果>10时,我想在一个小行的表中显示它,在一个正常的表结果中显示5到10个结果,在大div中显示<2个结果

有人能告诉我怎么算吗?
我还没有使用JQuery的经验。

data
是一个数组,而
data.length
是数组中的元素数。因此,您可以将其与不同的限制标准进行比较

if (data.length > 10) {
    // Code to show table with small rows
} else if (data.length >= 5) {
    // Code for normal table
} else if (data.length < 2) {
    // Code for big divs
} else {
    // Not sure what you want for 2-4, but this is where it goes
}
if(data.length>10){
//用于显示具有小行的表的代码
}否则如果(data.length>=5){
//标准表格代码
}否则如果(数据长度<2){
//大div的代码
}否则{
//不确定你想要什么2-4,但这就是它的方向
}

数据
是一个数组,而
数据。length
是数组中的元素数。因此,您可以将其与不同的限制标准进行比较

if (data.length > 10) {
    // Code to show table with small rows
} else if (data.length >= 5) {
    // Code for normal table
} else if (data.length < 2) {
    // Code for big divs
} else {
    // Not sure what you want for 2-4, but this is where it goes
}
if(data.length>10){
//用于显示具有小行的表的代码
}否则如果(data.length>=5){
//标准表格代码
}否则如果(数据长度<2){
//大div的代码
}否则{
//不确定你想要什么2-4,但这就是它的方向
}
var searchItem=function(){
$(“#搜索结果”).css('top','0px');
$.getJSON('/json/searchitem/q/'+$('#json_search')).val(),函数(数据){
$('#搜索结果ul').fadeIn().html('');
if($('search#u results table').css('display')=='none'){
$(“#搜索结果表”).fadeIn();
}
//检查结果长度是否小于2
如果(数据长度<2){
//以大比例表演
}否则{
//在带有小行的表格中显示它
$。每个(数据、功能(i、项){
$(“#搜索结果”).append(“”+item.code+“”);
});
}
});
};
var searchItem=function(){
$(“#搜索结果”).css('top','0px');
$.getJSON('/json/searchitem/q/'+$('#json_search')).val(),函数(数据){
$('#搜索结果ul').fadeIn().html('');
if($('search#u results table').css('display')=='none'){
$(“#搜索结果表”).fadeIn();
}
//检查结果长度是否小于2
如果(数据长度<2){
//以大比例表演
}否则{
//在带有小行的表格中显示它
$。每个(数据、功能(i、项){
$(“#搜索结果”).append(“”+item.code+“”);
});
}
});
};

我认为“数据”是代码片段中的一个数组。那么为什么不使用“data.length”?就像在data.length<10中一样?这在jQuery中是正确的格式吗?可能是题外话。。。但最好在将searchquery放入url之前先将其转义。你可以通过
encodeURIComponent($('.#json_search').val())
想想如果我搜索
oh/no/this/is/going/error会发生什么jQuery只是一个JavaScript库。您可以在jQuery中编写一些JavaScript代码。。。您可以使用
.empty()
而不是
.html(“”)
,我认为“数据”是代码段中的一个数组。那么为什么不使用“data.length”?就像在data.length<10中一样?这在jQuery中是正确的格式吗?可能是题外话。。。但最好在将searchquery放入url之前先将其转义。你可以通过
encodeURIComponent($('.#json_search').val())
想想如果我搜索
oh/no/this/is/going/error会发生什么jQuery只是一个JavaScript库。您可以在jQuery中编写一些JavaScript代码。。。您可以使用
.empty()
而不是
.html(“”)
谢谢,我不知道它的格式与我的php文件中的格式完全相同!Javascript和PHP从C中复制了大部分基本语法。谢谢,我不知道它的格式和我的PHP文件中的格式是一样的!Javascript和PHP复制了C语言的大部分基本语法。