【发布时间】: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