2016-04-01 24 views
2

私は現在、郵便番号の検索とリダイレクトスクリプトを作成するためにjQuery配列と条件文を使用しています。ユーザーが郵便番号を入力すると、スクリプトは配列からそのスクリプトを探し、それに応じてユーザーをページにリダイレクトします。jQuery郵便番号検索とリダイレクト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/'; 
    } 
}); 

私の質問:より少ないコードを書いている間に同じ機能を実現するより良い方法がありますか?私はまだjQueryを学んでいるので、皆さんのご意見をお待ちしております。ありがとう!

答えて

1

すべての郵便番号を、その場所をコードに結びつけるオブジェクトの配列に格納することができます。それはあなたの配列を検索し、該当する場所

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プロパティを変更しますに続いて、この

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

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

のようにフォーマットすることができ元の配列名southWestPdxの代わりにlocation: 'southwest-of-portaland'などの適切な名前を使用してください。

+0

驚くばかりです。ありがとうございます! –