Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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 If子句工作不正常_Javascript_Jquery_If Statement - Fatal编程技术网

Javascript If子句工作不正常

Javascript If子句工作不正常,javascript,jquery,if-statement,Javascript,Jquery,If Statement,如果jquery对象的值为空且dom元素不是标签或跨度,我想输入一个if。所以我有 $('.container').children().each(function (index, item2){ if ($(item2).val()=== '' && (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')){ //do stuff here

如果jquery对象的值为空且dom元素不是标签或跨度,我想输入一个if。所以我有

$('.container').children().each(function (index, item2){
    if ($(item2).val()=== '' && (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')){
        //do stuff here
        console.log("tag: "+item2.tagName.toLowerCase());
    }
});
但在控制台里我得到了

tag: label

这意味着它不能正常工作。我是否遗漏了某些内容?

如果要在值不为空的情况下输入条件,则需要使用
==而不是
==

if ($(item2).val() !== '' && (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')) {
    // your code...
}

如果要在值不为空时输入条件,则需要使用
==而不是
==

if ($(item2).val() !== '' && (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')) {
    // your code...
}

我会把它改写成

$('.container').children().each(function (index, item2){
    if ( item2.value ) {

    }
});

跨距或标签没有值,因此它们无论如何都将不符合条件

我会将其重写为

$('.container').children().each(function (index, item2){
    if ( item2.value ) {

    }
});

span或标签没有值,因此无论如何都会使条件失效

如果条件错误,请尝试以下操作:

$('.container').children().each(function() {
    if ($(this).val() !== '' && !$(this).is('span') && !$(this).is('label')) {
        console.log("tag: "+item2.tagName.toLowerCase());
    }
});
但是
span
label
没有
value
属性,如果要检查元素是否没有子元素(包括文本节点),则有选择器

$('.container').children().each(function() {
    if (!$(this).is(':empty, span, label')) {
        console.log(this);
    }
});

您的情况不正确,请尝试以下操作:

$('.container').children().each(function() {
    if ($(this).val() !== '' && !$(this).is('span') && !$(this).is('label')) {
        console.log("tag: "+item2.tagName.toLowerCase());
    }
});
但是
span
label
没有
value
属性,如果要检查元素是否没有子元素(包括文本节点),则有选择器

$('.container').children().each(function() {
    if (!$(this).is(':empty, span, label')) {
        console.log(this);
    }
});

您的代码是:

$('.container').children().each(function (index, item2){
   if ($(item2).val()=== '' && (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')){           
     console.log("tag: "+item2.tagName.toLowerCase());
   }
});
在这里编写您的条件:-
$(item2.val()==''&(item2.tagName.toLowerCase()!='label'| | item2.tagName.toLowerCase()!='span')

首先,如果要允许非空值元素,请使用
==而不是使用
=
(如@Rory McCrossan所建议的)

现在我们来讨论第二个条件,即-
(item2.tagName.toLowerCase()!='label'| | item2.tagName.toLowerCase()!='span')

这里的意思是,如果元素是
标签
SPAN
,则允许

所以,你的情况有以下四个方面-

(false || true ) ====>  true  // Element is label

(true || false ) ====>  true // Element is span

(true || true ) ====>  true // Element is not a span and not a label

(false || false ) ====>  false // Element is a span and also an label [this condition never satisfied] 
我想,你错了。您应该使用以下条件(如果您不允许两种类型的元素)-

$(item2.val()==''&&(item2.tagName.toLowerCase()!='label'&&item2.tagName.toLowerCase()!='span')

简而言之,您必须使用
&&/和
,而不是使用
| |/或

您的代码是:

$('.container').children().each(function (index, item2){
   if ($(item2).val()=== '' && (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')){           
     console.log("tag: "+item2.tagName.toLowerCase());
   }
});
在这里编写您的条件:-
$(item2.val()==''&(item2.tagName.toLowerCase()!='label'| | item2.tagName.toLowerCase()!='span')

首先,如果要允许非空值元素,请使用
==而不是使用
=
(如@Rory McCrossan所建议的)

现在我们来讨论第二个条件,即-
(item2.tagName.toLowerCase()!='label'| | item2.tagName.toLowerCase()!='span')

这里的意思是,如果元素是
标签
SPAN
,则允许

所以,你的情况有以下四个方面-

(false || true ) ====>  true  // Element is label

(true || false ) ====>  true // Element is span

(true || true ) ====>  true // Element is not a span and not a label

(false || false ) ====>  false // Element is a span and also an label [this condition never satisfied] 
我想,你错了。您应该使用以下条件(如果您不允许两种类型的元素)-

$(item2.val()==''&&(item2.tagName.toLowerCase()!='label'&&item2.tagName.toLowerCase()!='span')


简而言之,您必须使用
&&/和
,而不是使用
| |/或

它应该是
$(item2).val()!=''
。更正了它。我想输入if when值为空抱歉,它应该是
$(item2)。val()!=''
。更正了。我想输入if when值为空对不起,您还可以将标签/span测试组合到
$(this).is('span,label')
第二个问题是,即使有值,输入也总是空的,就像is方法不知道一样。您还可以将label/span测试与
结合起来$(this).is('span,label')
第二个的问题是,即使有值,输入也总是空的,就像is方法不知道一样it@A.Wolff-是的,一个空字符串是错误的,所以检查长度应该不是真的必要。这是这里最简洁的答案,投票次数较少(不包括我)。奇怪的是,有时…。@A.Wolff-是的,空字符串是错误的,所以检查长度应该不是真的必要。这是这里最简洁的答案,投票次数较少(不包括我)。奇怪,所以有时候。。。