jQuery:not()选择器不起作用。

时间:2022-08-10 19:39:08

I want to exclude selects with empty values from serialization.

我想从序列化中排除带空值的选择。

I'm trying to achieve this like so:

我试着做到这样:

    var form = $('#myForm');
    var str = $(':not(select[value=""])', form).serialize();

No erros, and the result is the entire form. The not() method gives the same.

没有erros,结果是整个表单。not()方法给出了相同的结果。

What is possibly wrong?

可能错的是什么?

EDIT: The question which was listed as possible duplicate to mine asks about possible implemntations for exlcuding empty fields frm serialization, while mine states that not() Selector doesn't work, asks why and for different to above mentioned solution.

编辑:列出的可能重复的问题是关于exlcuding空字段frm序列化的可能实现,而我的状态是not() Selector不能工作,问为什么和上面提到的解决方案不同。

1 个解决方案

#1


3  

Which filters element with element attribute value which equals to an empty string and not the element with current value as an empty string.

筛选具有元素属性值的元素,它等于一个空字符串,而不是将当前值作为空字符串的元素。


For filtering element with current value as empty string use filter() method.

var str = $(':input', form).filter(function(){
   return this.value.trim() != '';
}).serialize();


UPDATE : If you just want to avoid empty select tags then do it like.

var str = $(':input', form).filter(function(){
   return !($(this).is('select') && this.value.trim() == '');
}).serialize();

#1


3  

Which filters element with element attribute value which equals to an empty string and not the element with current value as an empty string.

筛选具有元素属性值的元素,它等于一个空字符串,而不是将当前值作为空字符串的元素。


For filtering element with current value as empty string use filter() method.

var str = $(':input', form).filter(function(){
   return this.value.trim() != '';
}).serialize();


UPDATE : If you just want to avoid empty select tags then do it like.

var str = $(':input', form).filter(function(){
   return !($(this).is('select') && this.value.trim() == '');
}).serialize();