【问题标题】:ajax call on maxLength - not updating on CTRL V (paste)maxLength 上的 ajax 调用 - 不在 CTRL V 上更新(粘贴)
【发布时间】:2023-03-19 19:52:02
【问题描述】:
//show city/state on input maxlength
$("input#billZipCode").live("keyup", function( event ){

    if(this.value.length == this.getAttribute('maxlength')) {
        if(!$(this).data('triggered')) {
            // set the 'triggered' data attribute to true
            $(this).data('triggered', true); 
            if ($(this).valid() == true ) { zipLookup(this, "USA"); } 
        }
    } else {
    $(this).data('triggered', false);
    }

});

函数 zipLookup 执行 ajax 调用并填充字段。

上述方法适用于用户输入邮政编码的情况 - 但是,如果用户输入邮政编码然后粘贴 (CTRL V) 新的邮政编码值,则该函数不会再次触发。

【问题讨论】:

  • 解决此问题的最简单方法是要求用户单击按钮或按回车键来调用它 - 否则您必须编写一些自定义代码来检测粘贴(从键盘和鼠标)以及打字。
  • @Utkanos - 是的,我想编写自定义代码来做到这一点 - 因此在 StackOverflow 上发布

标签: jquery ajax input maxlength


【解决方案1】:

你可能会捕捉到粘贴事件:

$(document).on('change keyup paste', '#billZipCode', function(){
   // do something
});​​​​​​

jquery 使用的onpaste 回调参考:https://developer.mozilla.org/en-US/docs/DOM/element.onpaste

请注意,正如 the documentation 中所指定的,这在 IE8 上不起作用:您必须将 paste 事件直接附加到元素:

在 Internet Explorer 8 及更低版本中,粘贴和重置事件不会 气泡。此类事件不支持与委派一起使用,但 当事件处理程序直接附加到 产生事件的元素。

旁注:

  • 使用“#billZipCode”而不是“input#billZipCode”
  • 使用on,而不是live(已弃用)

【讨论】:

  • 无法在 jQuery 1.6.2 中使用 .on - 但我假设方法是相同的
  • 是的,是一样的。但是你为什么不升级 jQuery 呢?
猜你喜欢
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
  • 2022-12-22
  • 2011-07-27
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 2020-12-14
相关资源
最近更新 更多