你的代码是:
$('.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麦克罗桑建议)。
现在我们谈谈你的第二个条件,即 - (item2.tagName.toLowerCase() !== 'label' || item2.tagName.toLowerCase() !== 'span')
意味着这里您允许,如果元素是LABEL
OR 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')
总之你必须使用&&/AND
而不是使用||/OR
。
它应该是'$(item2).val()!=='''。 – Andy
更正了它我想输入如果当值为空时对所有人抱歉 – Apostolos