简体   繁体   中英

Trigger change of input value without user interaction in the browser

I've two input fields, let's say #a and #b.

By clicking #a and inserting a char I fire an AJAX request. The response is filling the value of #b.

How can I detect any changes here?

The code above only works by doing directly something in the browser

$('#b').on('change input keyup', function(){
  var that = $(this);
  console.log('changed');
  if(that.val()){
    console.log(that.val());
  }
});

As listed on jquery docs change event :

Note: Changing the value of an input element using JavaScript, using .val() for example, won't fire the event.

So you will need to trigger your change event manually after your ajax success or convert the event to a function and just call it after ajax success.

Some developers went with setinterval way , but I think this is needed for special cases but just wanted to mention other options

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