提问者:小点点

当用户选择自动完成值时是否有事件触发?


如果我有一个带有自动完成字段的表单:

<form>
    <label>First name: <input type=text autocomplete="given-name"></label>
</form>

当用户从下拉菜单中选择自动完成值时(但在模糊事件之前)是否有事件触发?


共1个答案

匿名用户

您可以尝试使用window.getComputedStyle(elem).getPropertyValue(“background-color”)获取背景色,尽管这只在Chrome中起作用(据我所知,它是唯一一个在自动完成时更改输入背景色的浏览器)。

null

$(document).ready(function() {
  function getBgrColor(elem) {
    return window.getComputedStyle(elem).getPropertyValue("background-color");
  }
  var initial = getBgrColor($('input')[0]);
  $('input').on('blur input', function() {
    var bgr = getBgrColor($(this)[0]);
    if (bgr != initial) {
      console.log("Autofilled");
    }
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  Select an autocomplete option, then click outside the input
  <br><br>
  <label>First name: <input type=text name="name"
            autocomplete="given-name"></label>
</form>