jQuery邮政编码搜索和使用inArray重定向

时间:2016-04-01 15:35:24

标签: javascript jquery

我目前正在使用一堆jQuery数组和条件语句来创建邮政编码搜索和重定向脚本。当用户输入Zip代码时,脚本将从数组中查找它并相应地将用户重定向到页面。

// Portland
var mountain = ['97049', '97067', '97011'];
var east = ['97055', '97023', '97022', '97009', '97089'];
var southEast = ['97013', '97042', '97004', '97017', '97038'];
var i84Corridor = ['97019', '97014'];
var greshamNorthEast = ['97080', '97030', '97060', '97024', '97230', '97233', '97236', '97220', '97216', '97266', '97218', '97213', '97215', '97206', '97211', '97212', '97232', '97214', '97202', '97227', '97217', '97203'];
var southEastPdx = ['97222', '97267', '97015', '97086', '97045', '97027'];
var southWest = ['97002', '97137', '97071', '97032'];
var west = ['97114', '97127', '97115', '97132', '97111', '97148', '97128'];
var southWestPdx = ['97219', '97035', '97034', '97068', '97062', '97070', '97223', '97224', '97140'];
var northWestPdx = ['97204', '97205', '97209', '97201', '97210', '97221', '97239', '97231', '97229', '97225', '97005', '97008', '97006', '97007', '97051', '97053', '97056', '97109', '97133', '97124', '97106', '97116', '97125', '97119', '97123', '97113', '97018'];
var Wa = ['98671', '98607', '98675', '98604', '98606', '98682', '98684', '98683', '98662', '98664', '98686', '98665', '98663', '98660', '98685', '98661', '98642'];


$('#zipcodeSearch').submit(function(e){
    e.preventDefault();

    var root = location.protocol + '//' + location.host;
    var searchedZip = $('#zip-code').val();
    if(jQuery.inArray(searchedZip, mountain) > -1) {
        window.location.href = root + '/mountain/';
    } else if (jQuery.inArray(searchedZip, east) > -1) {
        window.location.href = root + '/east/';
    } else if (jQuery.inArray(searchedZip, southEast) > -1) {
        window.location.href = root + '/southeast/';
    } else if (jQuery.inArray(searchedZip, i84Corridor) > -1) {
        window.location.href = root + '/i-84-corridor/';
    } else if (jQuery.inArray(searchedZip, greshamNorthEast) > -1) {
        window.location.href = root + '/gresham-northeast/';
    } else if (jQuery.inArray(searchedZip, southEastPdx) > -1) {
        window.location.href = root + '/southeast-of-portland/';
    } else if (jQuery.inArray(searchedZip, southWest) > -1) {
        window.location.href = root + '/southwest/';
    } else if (jQuery.inArray(searchedZip, west) > -1) {
        window.location.href = root + '/west/';
    } else if (jQuery.inArray(searchedZip, southWestPdx) > -1) {
        window.location.href = root + '/southwest-of-portland/';
    } else if (jQuery.inArray(searchedZip, northWestPdx) > -1) {
        window.location.href = root + '/northwest-of-portland/';
    } else if (jQuery.inArray(searchedZip, Wa) > -1) {
        window.location.href = root + '/wa/';
    }
    else {
        window.location.href = root + '/service-areas/';
    }
});

我的问题:在编写更少的代码时,是否有更好的方法来实现相同的功能?我还在学习jQuery,所以我很感激你的所有投入。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以将所有邮政编码存储在将其位置与代码相关联的对象数组中。它可以像这样格式化

var zips = [{ location: 'mountain', zipCodes: ['97049', '97067', '97011'] },
            { location: 'east', zipCodes: ['97055', '97023', '97022', '97009', '97089']}];

//one entry for each array you previously had...

然后在提交时,您可以搜索阵列并找到相应的位置

var root = location.protocol + '//' + location.host;
var searchedZip = $('#zip-code').val();

for(var i = 0; i < zips.length; i++){
  if(zips[i].zipCodes.indexOf(searchedZip) > -1){
    window.location.href = root + '/' + zips[i].location + '/';
    break;
  }
}

您可以更改location中每个对象的zips属性,以确保其具有正确的名称,例如location: 'southwest-of-portaland',而不是原始数组名称southWestPdx。< / p>