1

这里是相当新的用户,但我在获取 ziptastic 时遇到了问题,而且它是在 IE 中工作的 jquery。可以在所有其他浏览器中正常工作。这是 HTML 和 jquery:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
        <title>Zip Code Lookup Demo</title>
        <style>
            form label, form input {
                display: block;
            }
        </style>
    </head>
    <body>
        <form id="theform">
            <label for="zip">
                Zip:
                <input type="text" id="zip" />
            </label>

            <label for="city">
                City:
                <input type="text" id="city" />
            </label>

            <label for="state">
                State:
                <input type="text" id="state" />
            </label>

            <label for="country">
                Country:
                <input type="text" id="country" />
            </label>

            <input type="submit" />
        </form>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
        <script type="text/javascript" src="jquery.ziptastic.js"></script>
        <script type="text/javascript">
            (function($) {
                $(function() {
                    var duration = 500;

                    var elements = {
                        country: $('#country'),
                        state: $('#state'),
                        city: $('#city'),
                        zip: $('#zip')
                    }

                    // Initially hide the city/state/zip
                    elements.country.parent().hide();
                    elements.state.parent().hide();
                    elements.city.parent().hide();

                    // Initialize the ziptastic and bind to the change of zip code
                    elements.zip.ziptastic()
                        .on('zipChange', function(evt, country, state, city, zip) {
                            elements.country.val(country).parent().show(duration);
                            elements.state.val(state).parent().show(duration);
                            elements.city.val(city).parent().show(duration);
                        });
                });
            }(jQuery));
        </script>
    </body>
</html>

至于jquery:

(function( $ ) {
    var requests = {};
    var zipValid = {
        us: /[0-9]{5}(-[0-9]{4})?/
    };

    $.ziptastic = function(zip, callback){
        // Only make unique requests
        if(!requests[zip]) {
            requests[zip] = $.getJSON('http://zip.elevenbasetwo.com/v2/US/' + zip);
        }

        // Bind to the finished request
        requests[zip].done(function(data) {
            callback(data.country, data.state, data.city, zip);
        });

        // Allow for binding to the deferred object
        return requests[zip];
    };

    $.fn.ziptastic = function( options ) {
        return this.each(function() {
            var ele = $(this);

            ele.on('keyup', function() {
                var zip = ele.val();

                // TODO Non-US zip codes?
                if(zipValid.us.test(zip)) {
                    $.ziptastic(zip, function(country, state, city) {
                        // Trigger the updated information
                        ele.trigger('zipChange', [country, state, city, zip]);
                    })
                }
            });
        });
    };
})( jQuery );

为什么 IE 无法处理这个问题?在 Safari、Chrome、Firefox 上运行良好?任何帮助是极大的赞赏..

4

0 回答 0