2016-04-01 16 views
2

Şu anda bir Posta Kodu arama ve yönlendirme komut dosyası oluşturmak için jQuery dizileri ve koşullu deyimleri bir grup kullanıyorum. Bir kullanıcı bir Zip Kodu girdiğinde, betik dizilerden onu arar ve kullanıcıyı buna göre bir sayfaya yönlendirir.jQuery Posta Kodu arama ve inArray ile yeniden yönlendirme

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

Soruma Soru: Daha az kod yazarken aynı işlevi yerine getirmenin daha iyi bir yolu var mı? JQuery'yi hala öğreniyorum, bu yüzden tüm girdilerinizi takdir ediyorum. Teşekkürler!

cevap

1

Tüm posta kodlarını, konumlarını kodla ilişkilendiren bir nesne dizisinde depolayabilirsiniz. Sonra da dizi arama yapabilirsiniz göndermek ve sahip olduğu emin olmak için

Sen zips her nesnenin location özelliğini değiştirecek karşılık gelen konumu

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

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

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

gibi biçimlendirilmiş olabilir Orijinal dizi adı southWestPdx yerine location: 'southwest-of-portaland' gibi uygun ad.

+0

Harika. Teşekkürler bayım! –