2016-04-01 7 views
2

Ich verwende derzeit eine Reihe von jQuery-Arrays und bedingten Anweisungen, um eine Suche und Umleitung Skript für die Postleitzahl zu erstellen. Wenn ein Benutzer eine Postleitzahl eingibt, sucht das Skript danach in den Feldern und leitet den Benutzer entsprechend auf eine Seite um.jQuery PLZ suchen und umleiten mit inArray

// 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/'; 
    } 
}); 

Meine Frage: Gibt es einen besseren Weg, um die gleiche Funktionalität zu erreichen, während weniger Code zu schreiben? Ich lerne immer noch jQuery, also würde ich alle Ihre Eingaben zu schätzen wissen. Vielen Dank!

Antwort

1

Sie können alle Postleitzahlen in einem Array von Objekten speichern, die ihre Position an den Code binden. Es könnte wie dieses

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

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

Sie Ihr Array und den entsprechenden Ort

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

Sie die location Eigenschaft jedes Objekts in zips finden Suche kann dann anschließend auf formatiert werden, um sicherzustellen, dass es das würde sich ändern Eigenname wie location: 'southwest-of-portaland' anstelle des ursprünglichen Array-Namen southWestPdx.

+0

Ehrfürchtig. Danke mein Herr! –