邮寄地址和国家/地区特定的表单字段JSON手机

时间:2013-02-21 19:54:39

标签: html json

他都是,有一个最新的网站,公开国家特定的邮寄地址和表格字段JSON中的手机#s所以我可以迭代JSON,创建HTML并始终拥有我的地址&我的网站上的电话号码字段是最新的? 理想情况下,每个字段都可以映射到每个国家/地区的类似字段,以鼓励正确的数据结构。

谢谢! 布赖恩

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的http://weblogs.asp.net/awilinsk/archive/2009/08/13/jquery-amp-international-address-data-collecting-and-storing.aspx

取自上述来源:

(function($) {
    var _fields = {
        address1: { id: "address1", name: "address1" },
        address2: { id: "address2", name: "address2" },
        address3: { id: "address3", name: "address3" },
        locality: { id: "locality", name: "locality" },
        region: { id: "region", name: "region" },
        postCode: { id: "postCode", name: "postCode" }
    };
    var _labels = {
        address1: "Address:",
        address2: "",
        address3: "",
        area: "Area:",
        city: "City:",
        county: "County:",
        department: "Department:",
        district: "District:",
        emirate: "Emirate:",
        island: "Island:",
        locality: "City/Town:",
        metroProvince: "Metro/Province:",
        region: "State/Province/Region:",
        postalCode: "Postal Code:",
        postCode: "Zip/Postal Code:",
        prefecture: "Prefecture:",
        province: "Province:",
        state: "State:",
        zip: "Zip:"
    };
    var _layouts = {};
    var _templates = {
        text: function(data) {
            return "<label for=\"" + data.id + "\">" + data.label + "</label><input id=\"" + data.id + "\" name=\"" + data.name + "\" type=\"text\"/>";
        },
        select: function(data) {
            var html = "<label for=\"" + data.id + "\">" + data.label + "</label><select id=\"" + data.id + "\" name=\"" + data.name + "\" disabled=\"disabled\"><option selected=\"selected\">Loading...</option></select>";
            data.source(function(regions) {
                var html2 = "";
                $.each(regions, function(i, item) {
                    html2 += "<option value=\"" + item.value + "\">" + item.text + "</option>";
                });
                $("#" + data.id).removeAttr("disabled").html(html2);
            });
            return html;
        }
    };
    var _defaultLayout = [
        { fieldName: "address1", templateName: "text", labelName: "address1" },
        { fieldName: "address2", templateName: "text", labelName: "address2" },
        { fieldName: "address3", templateName: "text", labelName: "address3" },
        { fieldName: "locality", templateName: "text", labelName: "locality" },
        { fieldName: "region", templateName: "text", labelName: "region" },
        { fieldName: "postCode", templateName: "text", labelName: "postCode" }
    ];
    $.fn.globalAddress = function(countrySelect, settings) {
        settings = $.extend(true, {}, {
            labels: _labels,
            fields: _fields,
            templates: _templates
        }, settings);
        var addressContainer = $(this);
        $(countrySelect).change(function(e) {
            var country = $(this).val();
            var layout = _layouts[country] ? _layouts[country] : _defaultLayout;
            var html = "";
            $.each(layout, function(i, l) {
                var data = $.extend(true, { label: _labels[l.labelName] }, settings.fields[l.fieldName], l.data);
                html += settings.templates[l.templateName](data);
            });
            $(addressContainer).html($(html));
        }).trigger("change");
        return this;
    };
    $.globalAddressExtend = function(key, layout) {
        _layouts[key] = layout;
    };
})(jQuery)

用法:

(function($) {
    $.globalAddressExtend("US", [
        { fieldName: "address1", templateName: "text", labelName: "address1" },
        { fieldName: "address2", templateName: "text", labelName: "address2" },
        { fieldName: "address3", templateName: "text", labelName: "address3" },
        { fieldName: "locality", templateName: "text", labelName: "city" },
        { fieldName: "region", templateName: "text", labelName: "state", source: function(callback) {
            //get data here and call the callback function
        } },
        { fieldName: "postCode", templateName: "text", labelName: "zip" }
    ]);
    $.globalAddressExtend("CA", [
        { fieldName: "address1", templateName: "text", labelName: "address1" },
        { fieldName: "address2", templateName: "text", labelName: "address2" },
        { fieldName: "address3", templateName: "text", labelName: "address3" },
        { fieldName: "locality", templateName: "text", labelName: "city" },
        { fieldName: "region", templateName: "text", labelName: "province", source: function(callback) {
            //get data here and call the callback function
        } },
        { fieldName: "postCode", templateName: "text", labelName: "postalCode" }
    ]);
});