Why Jquery selector by 'value' not work in case of dynamic change

后端 未结 3 1331
误落风尘
误落风尘 2020-12-19 21:32

I\'m not sure why jquery selector value not work, Trying to change the value of inputs to \"a\" but the length not increment, please c

3条回答
  •  遥遥无期
    2020-12-19 21:59

    If you are changing value dynamically it wouldn't get selected by attribute selector. You can use filter() instead.

    Attribute selector will not check the dom node's value property it only targets the element's attribute

    $('body').on('input', '.example', function() {
      $('#result').text($('.example').filter(function() {
        return this.value == 'a'
      }).length);
    });
    
    
    
    
    
    


    Or you need to manually update the element attribute on input event

    $('body').on('input', '.example', function() {
      $(this).attr('value', this.value);
      $('#result').text($('.example[value="a"]').length);
    });
    
    
    
    
    
    

提交回复
热议问题