在x字符Ajax调用 [英] ajax call after x characters

查看:116
本文介绍了在x字符Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ajax调用触发输入的最大长度。但是,它继续如果用户继续输入字符(因为它们算作一个关键preSS)来触发。有没有什么办法prevent额外的Ajax调用5个字符的限制?

HTML

 <输入类型=文字值=ID =billZip codeNAME =billZip code级=smallInput coreAddrBill最大长度= 5>
 

的javascript

输入最大长度

  //展示城市/州
$(#输入billZip code)。生活(KEYUP功能(事件){
    如果(this.value.length == this.getAttribute(最大长度)){
        如果($(本).valid()==真){
            zipLookup(这一点,美国);

        }
    }
});
 

解决方案

设置在该领域的 .DATA 属性:

输入最大长度

  //展示城市/州
$(#输入billZip code)。生活(KEYUP功能(事件){

    如果(this.value.length == this.getAttribute(最大长度)){
        如果(!$(本)的.d​​ata('触发')){
            //设置'触发'data属性为true
            $(本)的.d​​ata('触发',真正的);
            如果($(本).valid()==真){zipLookup(这一点,美国); }
        }
    }其他{$(本)的.d​​ata('触发',假); }

});
 

将第二个如果语句中的赋值,如果你想继续运行,直到拉链code是有效的。

I have an ajax call triggering on the maxlength of an input. However, it continues to trigger if a user continues to enter characters (because they count as a keypress). Is there any way to prevent additional ajax calls after the 5 character limit?

html

<input type="text" value="" id="billZipCode" name="billZipCode" class="smallInput coreAddrBill" maxlength="5">

javascript

//show city/state on input maxlength
$("input#billZipCode").live("keyup", function( event ){
    if(this.value.length == this.getAttribute('maxlength')) {
        if ($(this).valid() == true ) {
            zipLookup(this, "USA");

        }
    }
});

解决方案

Set a .data attribute on the field:

//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); }

});

Move the assignment inside the second if statement if you want to keep running this until the zipcode is valid.

这篇关于在x字符Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆