2015-09-07 19 views
0

元素,我需要找到所有的表單元素和排除空字段,並選擇與元素值-1找到所有表單元素和排除空字段,並選擇與值-1

我已經試過這

$('form#myForm').find(":input[value!=''][value!='-1']"); 

這適用於所有的元素,但我需要申請value!=-1條件只對選擇元素

+0

你的意思是說下拉列表中選擇元素時? –

+0

@MilindAnantwar是的,我的意思是下拉菜單 –

+0

在下面添加更新的答案.... –

回答

1

你不應該使用「標籤#身份證」也不是「#ID otherSelector」選擇看http://lab.abhinayrathore.com/jquery-standards/#Selectors

var $notEmptyInputs = $('#myForm').find(':input').filter(function(){ 
    var include = true; 
    if (this.tagName == 'SELECT') { 
     include = this.value !== '-1'; 
    } else { 
     include = !!this.value; 
    } 
    return include; 
}) 
0

是不是這樣的:

$('form#myForm').find(":input[value='-1']"); 

這將讓您僅過濾那些僅具有值-1的元素。

0

如果值是硬編碼的,你可以使用:

$('form#myForm :input[value="-1"]'); 

其他:

$('form#myForm :input').filter(function(){return this.value=='-1'}) 
0

您可以遍歷所有的輸入和做任何你想做

$('form#myForm :input[value!=""]').each(function() { 
    var itemValue = this.val(); 
    if(itemValue != "-1" || itemValue == "Y") 
    alert("Item value is " + itemValue); 
}); 
1

您需要使用:

$("form#myForm select").filter(function(){ 
    return $(this).val() != -1; 
}).add("form#myForm :input[value!='']:not(select)");