Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查jQuery.each()是否为空_Javascript_Jquery - Fatal编程技术网

Javascript 如何检查jQuery.each()是否为空

Javascript 如何检查jQuery.each()是否为空,javascript,jquery,Javascript,Jquery,如何检查jQuery.each()函数是否找不到内容,然后将txt值更改为“默认值” $(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active').each(function(i) { var val = $(this).val(); if (i == 0) { txt = val; } else { txt

如何检查jQuery.each()函数是否找不到内容,然后将txt值更改为“默认值”

$(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active').each(function(i) {
    var val = $(this).val();

    if (i == 0) {
        txt = val;
    } else {
        txt = txt + ', ' + val;
    }
});
我已经试过了

if (i == null) {
    txt = 'default';
}

但是它不起作用

在jquery中使用
.length
来获取长度

var lnt = $(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active');
if(lnt.length > 0) {

lnt.each(function(i) {
    var val = $(this).val();

    if (i == 0) {
        txt = val;
    } else {
        txt = txt + ', ' + val;
    }
});

}

使用.length并检查其是否为空

var objcollection = $(this).closest('.filter-select__dropdown-inner').
         find('> .button--checkbox.button--active');

if(objcollection.length==0)
{
}
else
{
}

i.length==0
它不是work@PutraFajarHasanuddin,这可能是一个上下文问题。如果您试图查找要迭代的数组的大小,最好使用:
$(this).closest('.filter-select\uu dropdown-inner').find('>.button--checkbox.button--active')。length
。在您描述的上下文中使用
i
将返回当前迭代的数组项的第一个参数的长度。如果为空,请稍候,将文本设置为“默认值”?什么文本?你也可以这样尝试:我编辑答案是为了利用变量来提高效率。