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

jQuery Zip Code search and redirect with inArray

本文关键字:重定向 邮政编码 搜索 使用 inArray jQuery      更新时间:2023-11-25

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

// 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,所以我非常感谢您的意见。谢谢

您可以将所有邮政编码存储在一个对象数组中,将它们的位置与代码联系起来。它可以像这样格式化

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

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