简体   繁体   中英

jQuery attribute filter to get textbox based on a value

I am trying to get all empty text boxes on my page using jQuery. Somehow I am not able to get all the text boxes that are empty.

I have three text boxes like this

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

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

if i have this

<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.

if I type "a" in the second textbox then $(".test[value='a']") -- this still returns only one control.

What wrong am I doing here?

You should be searching where there is no value attribute, like this:

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

or

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

Based on your comments it seems that you want not to get the initial list of elements, but also be able to re-run the selector when someone inputs the value. The issue here is that if you enter the text to input, it does not add value attribute to the DOM. In this case you can use approach similar to what is described in this answer :

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

This approach checks the value of the input, not the DOM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM