jQuery Ajax调用在字符删除上的更新 [英] JQuery ajax call updates on character deletion

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

问题描述

我有一个小的jQuery函数,它根据邮政编码输入字段从服务器收集数据:

I have a small jQuery function which collects data from the server depending on a zipcode input field:

$(document).ready(function(){
  $('#id_zipcode').keyup(function() {
    var selected = this.value;
    if (selected.length > 3) {
        $.getJSON(get_zip_factors_url, {
            zipcode: selected
        }, function(env_data, jqXHR) {
            $('#snow_fall').html(env_data[0].snow);
            $('#wind_speed').html(env_data[0].wind);
        });
    }
  }).keyup();
});

现在工作正常.但是,如果用户删除字符,则为#snow_fall#wind_speed呈现的值将保持不变.当用户删除导致服务器没有返回有效数据的字符时,是否可以通过某种方式注册此删除操作,并将值还原为不可用"字符串?

This works fine now. However, if the user deletes characters the values rendered for #snow_fall and #wind_speed persist. Is there some way I can register this deletion and revert the values to say a 'not available' string when the user deletes characters which result in no valid data being returned from the server?

非常感谢您的帮助.

推荐答案

如果要查找邮政编码值,为什么不更改为if(selected.length == 5),然后使用if语句的else进行设置snow_fall和wind_speed变为不可用"?

If you're looking for zip code values why not change to if(selected.length == 5) and then use the else of the if statement to set snow_fall and wind_speed to "not available"?

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

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