2012-05-23 204 views
21

此循环跳过某些键的语法是什么?我写的方式工作不正常。if语句中的jQuery多重条件

$.each(element, function(i, element_detail){ 
    if (!(i == 'InvKey' && i == 'PostDate')) { 
     var detail = element_detail + ' '; 
     $('#showdata').append('<div class="field">' + i + detail + '</div>'); 
     } 
}); 
+1

为什么没有按”它的工作?怎么了? – SLaks

+0

'if(i =='InvKey'|| i =='PostDate'){'? –

回答

48

尝试

if (!(i == 'InvKey' || i == 'PostDate')) { 

if (i != 'InvKey' || i != 'PostDate') { 

说,如果我不等于InvKey OR PostDate

+0

“If'i'不等于'InvKey'或'PostDate'“。 –

+0

Udpated我的回答 –

+0

确定快速的手指,我得到我的格式化lol –

10

i == 'InvKey' && i == 'PostDate'将永远是真实的,因为i绝不能等于两个不同的东西一次。

你可能会试图写

if (i !== 'InvKey' && i !== 'PostDate')) 
+0

将'''使它总是如此...... –

+1

'我'可能不是基于他正在迭代的索引.. –

+0

@ Vega#1:的确如此。因此它不会做他想做的事。 – SLaks