Input event not working if value is changed with jQuery val() or JavaScript

后端 未结 7 2167
孤城傲影
孤城傲影 2020-12-28 16:12

If I change the value of an input field programmatically, the input and change events are not firing. For example, I have this scenario:

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 16:43

    var $input = $('#myinput');
    
    $input.on('input', function() {
      // Do this when value changes
      alert($input.val());
    });
    
    $('#change').click(function() {
      // Change the value
      $input.val($input.val() + 'x');
    });
    
    
    
    

提交回复
热议问题