jQuery属性过滤器,用于根据值获取文本框

时间:2015-01-18 07:42:27

标签: javascript jquery

我正在尝试使用jQuery在我的页面上获取所有空文本框。不知怎的,我无法获得所有空的文本框。

我有三个这样的文本框

<input type="text" class="test" />
<input type="text" class="test" />
<input type="text" class="test" />

$(".test[value='']") -- this does not returns anything.

如果我有这个

<input type="text" class="test" value="a" />
<input type="text" class="test" />
<input type="text" class="test" />

$(".test[value='a']") -- this returns one control which is correct.
$(".test[value='']") -- this still returns nothing.

如果我输入&#34; a&#34;然后在第二个文本框中     $(&#34; .test [value =&#39; a&#39;]&#34;) - 这仍然只返回一个控件。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

您应该搜索没有value属性的地方,例如:

$('.test:not([value])')

$('.test'):not('[value]')

根据您的评论,您似乎不想获取元素的初始列表,但也可以在有人输入值时重新运行选择器。这里的问题是,如果输入要输入的文本,则不会向DOM添加value属性。在这种情况下,您可以使用类似于this answer中描述的方法:

$('input:text').filter(function() { return this.value == ""; })

此方法检查输入的值,而不是DOM。

相关问题