JQuery value attribute selector does not return the correct number of elements

I have a program that generates an account after the key is up. JQuery Code:

$('.today').keyup(function() {
    var Presents = $('input[value="/"]:visible');
    $("#counter").html( "Present: " + Presents.length );
});

HTML:

<input type="text" id="1" name="1" class="today" value="/">
<input type="text" id="2" name="2" class="today" value="/">
<input type="text" id="3" name="3" class="today" value="/">
<p id="counter"></p>

Tag counter will be displayed 3 after the first key press. When I change the value in the text fields, the value does not change in the counter field. EG. when I lose the value of the text box 3 through x, the tag

should now contain number 2. This is not currently changing.

+4
source share
1 answer

, ; . filter(), , :

$('.today').keyup(function() {
    var Presents = $('input:visible').filter(function(){
      return this.value == "/";  
    });
    $("#counter").html( "Present: " + Presents.length );
});

JSFiddle

, , :

$(this).attr('value',this.value);

JSFiddle

. , , filter() , .

+6

Source: https://habr.com/ru/post/1533585/


All Articles