简体   繁体   English

触发输入值的更改,而无需用户在浏览器中进行交互

[英]Trigger change of input value without user interaction in the browser

I've two input fields, let's say #a and #b. 我有两个输入字段,例如#a和#b。

By clicking #a and inserting a char I fire an AJAX request. 通过单击#a并插入一个字符,我会发出AJAX请求。 The response is filling the value of #b. 响应正在填充#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 : 如jquery docs change event上所列:

Note: Changing the value of an input element using JavaScript, using .val() for example, won't fire the event. 注意:例如,使用JavaScript更改输入元素的值,例如使用.val()不会触发该事件。

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. 因此,您需要在ajax成功之后手动触发您的change事件,或者将事件转换为函数,然后在ajax成功之后调用它。

Some developers went with setinterval way , but I think this is needed for special cases but just wanted to mention other options 一些开发人员采用setinterval方式 ,但是我认为这在特殊情况下是必需的,但只想提及其他选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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