最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery event upon selection of a suggested item on a text box? - Stack Overflow

programmeradmin3浏览0评论

I'd like to run a function upon each modification of a textbox control as soon as the user is typing. the event .keyup() is fine for most of the cases.

However, browsers (like Chrome or Firefox) may suggest autocompletion entries for textbox controls (probably because the @name or @id of the input control is known).

Unfortunately, I can't have any of the below events fired when "clicking on a suggested entry". ( .keyup() fires when selected with keyboard )

$('input#email')
  .click(function() { console.log('click'); })
  .keyup(function() { console.log('keyup'); })
  .keydown(function() { console.log('keydown'); })
  .change(function() { console.log('change'); })
  .focus(function() { console.log('focus'); })
  .blur(function() { console.log('blur'); });

As much as possible, I'd like to avoid using a setInterval() periodical check.

Is there a way to detect this "select a suggestion" event ?

I'd like to run a function upon each modification of a textbox control as soon as the user is typing. the event .keyup() is fine for most of the cases.

However, browsers (like Chrome or Firefox) may suggest autocompletion entries for textbox controls (probably because the @name or @id of the input control is known).

Unfortunately, I can't have any of the below events fired when "clicking on a suggested entry". ( .keyup() fires when selected with keyboard )

$('input#email')
  .click(function() { console.log('click'); })
  .keyup(function() { console.log('keyup'); })
  .keydown(function() { console.log('keydown'); })
  .change(function() { console.log('change'); })
  .focus(function() { console.log('focus'); })
  .blur(function() { console.log('blur'); });

As much as possible, I'd like to avoid using a setInterval() periodical check.

Is there a way to detect this "select a suggestion" event ?

Share Improve this question edited Sep 13, 2021 at 11:56 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jun 15, 2012 at 10:35 MyobisMyobis 1,35717 silver badges27 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

bind propertychange event:

$('input').bind('input propertychange', function() { 

    var input = this.value.trim();
    [...]   

});

The short answer is no, there is no event for detecting a suggestion selection. You could try looking at DOM mutation observers but I'm uncertain if these cover attribute changes, and there is little support for this API so far anyway.

So if you really need to handle this case then I think setInterval is your only option.

Edit: 3 years later you can use propertychange with jQuery as in the new accepted answer (which I assume uses mutation observers under the hood).

alternative solution disable autocomplete and handle other events like keyup,keydown etc

<input type="text" name="foo" autocomplete="off" /> 

The change event seems to works fine, but does require you to click away

发布评论

评论列表(0)

  1. 暂无评论