2015-09-07 18 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)");