如何使用国家/地区代码和产品代码制作数组和for循环

时间:2014-04-13 23:13:18

标签: javascript html

我有国家/地区代码和产品代码,根据选择的国家/地区组合,与其他国家/地区相比,该组国家/地区的产品代码会有所不同。

总共有13个产品代码,这些代码都是下面下拉列表中的所有默认值。 (405,406,407,408,409,410,411,412,413,414,415,416,417)这些都是美国,加拿大和加拿大国家的正确产品代码。

因此,如果有人从下拉列表中选择产品代码但不是来自其中一个国家/地区,那么我需要将产品代码下拉列表中的值与指定的同一产品中的另一个值相对应国家部分。

有4个国家/地区(默认,nfr,eu和jp)

如何使用数组将国家/地区代码存储在各个部分中,例如:

var usca = ["US", "CA", "PH"];
var nfr = ["AU", "BS", "HK", "MX", "NZ", "SG", "TH"];
var eu = ["AT", "BE", "BG", "CY", "CZ", "DK", "EE"];
var jp = "JP";

然后在我的javascript函数中检索我的else语句中的值。我不知道如何为此编写for循环。

另外,我需要在第一个else语句下面添加一些其他语句,例如:

在我的下面的JavaScript中,AU,BS,HK,MX,NZ,SG和TH国家/地区的产品代码 410 为485.但相同的产品代码 410 对于国家AT,BE,BG,CY,CZ,DK和EE为605。

到目前为止,这是我的JavaScript函数和选项选择下拉列表。

    function getCodes(){
    var countryCode = document.getElementById("country").value;
    var productCode = document.getElementById("products").value;
    var output = document.getElementById("output");
    if ((countryCode == "US") || (countryCode == "CA") || (countryCode == "PH")) {
        output.innerHTML = productCode + " " + countryCode;
    }else{
        if ((countryCode == "AU") || (countryCode == "BS") || (countryCode == "HK") || (countryCode == "MX") || (countryCode == "NZ") || (countryCode == "SG") || (countryCode == "TH") && (productCode == "410")) {
            productCode = "485";
            output.innerHTML = productCode + " " + countryCode;
        }

    }

}


<select name="country" id="country">
  <option value="#" selected="selected">Select Your Country</option>
  <option value="US">United States</option>
  <option value="CA">Canada</option>
  <option value="AU">Australia</option>
  <option value="AT">Austria</option>
  <option value="BS">Bahamas</option>
  <option value="BE">Belgium</option>
  <option value="BG">Bulgaria</option>
  <option value="CY">Cyprus</option>
  <option value="CZ">Czech Republic</option>
  <option value="DK">Denmark</option>
  <option value="EE">Estonia</option>
  <option value="FI">Finland</option>
  <option value="FR">France</option>
  <option value="DE">Germany</option>
  <option value="GR">Greece</option>
  <option value="HK">Hong Kong</option>
  <option value="HU">Hungary</option>
  <option value="IL">Israel</option>
  <option value="IT">Italy</option>
  <option value="LV">Latvia</option>
  <option value="LT">Lithuania</option>
  <option value="LU">Luxembourg</option>
  <option value="MT">Malta</option>
  <option value="MX">Mexico</option>
  <option value="NZ">New Zealand</option>
  <option value="NL">Netherlands</option>
  <option value="NO">Norway</option>
  <option value="PH">Philippines</option>
  <option value="PL">Poland</option>
  <option value="PT">Portugal</option>
  <option value="RO">Romania</option>
  <option value="RU">Russia</option>
  <option value="SG">Singapore</option>
  <option value="SK">Slovakia</option>
  <option value="SL">Slovenia</option>
  <option value="ES">Spain</option>
  <option value="SE">Sweden</option>
  <option value="CH">Switzerland</option>
  <option value="TH">Thailand</option>
  <option value="TR">Turkey</option>
  <option value="GB">United Kingdom</option>
</select>


<select name="products" id="products">
  <option value="#" selected="selected">Select Combo Boxes</option>
  <option value="410">1 box product A</option>
  <option value="405">2 boxes product A</option>
  <option value="406">4 boxes product A</option>
  <option value="409">1 box product B</option>
  <option value="407">2 boxes product B</option>
  <option value="408">4 boxes product B</option>
  <option value="413">1 box product C</option>
  <option value="414">2 boxes product C</option>
  <option value="415">4 boxes product C</option>
  <option value="411">2 boxes product D</option>
  <option value="412">4 boxes product D</option>
  <option value="416">2 boxes product E</option>
  <option value="417">4 boxes product E</option>
</select>

所以基本上我要做的是:当用户从下拉列表中选择一个国家然后选择一个产品时,会有一个添加到购物车按钮,按下后会将它们发送到正确的网址国家/地区代码和产品代码的网址中的参数。问题是,下拉列表中的默认产品代码对于所有国家/地区都不相同。

这里有什么好的解决方案?


修改

<script>
function getCodes(){
    var countryCode = document.getElementById("country").value;
    var productCode = document.getElementById("products").value;
    var output = document.getElementById("output");
    var specialCodes = {
    AU410: '485', AU405: '480', AU406: '481', AU409: '484', AU407: '482', AU408: '483', AU413: '492', AU414: '488', AU415: '489', AU411: '486', AU412: '487', AU416: '490', AU417: '491',
    BS410: '485', BS405: '480', BS406: '481', BS409: '484', BS407: '482', BS408: '483', BS413: '492', BS414: '488', BS415: '489', BS411: '486', BS412: '487', BS416: '490', BS417: '491',
    HK410: '485', HK405: '480', HK406: '481', HK409: '484', HK407: '482', HK408: '483', HK413: '492', HK414: '488', HK415: '489', HK411: '486', HK412: '487', HK416: '490', HK417: '491',
    MX410: '485', MX405: '480', MX406: '481', MX409: '484', MX407: '482', MX408: '483', MX413: '492', MX414: '488', MX415: '489', MX411: '486', MX412: '487', MX416: '490', MX417: '491',
    NZ410: '485', NZ405: '480', NZ406: '481', NZ409: '484', NZ407: '482', NZ408: '483', NZ413: '492', NZ414: '488', NZ415: '489', NZ411: '486', NZ412: '487', NZ416: '490', NZ417: '491',
    SG410: '485', SG405: '480', SG406: '481', SG409: '484', SG407: '482', SG408: '483', SG413: '492', SG414: '488', SG415: '489', SG411: '486', SG412: '487', SG416: '490', SG417: '491',
    TH410: '485', TH405: '480', TH406: '481', TH409: '484', TH407: '482', TH408: '483', TH413: '492', TH414: '488', TH415: '489', TH411: '486', TH412: '487', TH416: '490', TH417: '491',
    JP410: '516', JP405: '516', JP406: '522', JP409: '517', JP407: '517', JP408: '523', JP413: '528', JP414: '528', JP415: '530', JP411: '521', JP412: '521', JP416: '534', JP417: '534',
    AT410: '605', AT405: '600', AT406: '601', AT409: '604', AT407: '602', AT408: '603', AT413: '605', AT414: '600', AT415: '601', AT411: '617', AT412: '618', AT416: '617', AT417: '618',
    BE410: '605', BE405: '600', BE406: '601', BE409: '604', BE407: '602', BE408: '603', BE413: '605', BE414: '600', BE415: '601', BE411: '617', BE412: '618', BE416: '617', BE417: '618',
    BG410: '605', BG405: '600', BG406: '601', BG409: '604', BG407: '602', BG408: '603', BG413: '605', BG414: '600', BG415: '601', BG411: '617', BG412: '618', BG416: '617', BG417: '618',
    CY410: '605', CY405: '600', CY406: '601', CY409: '604', CY407: '602', CY408: '603', CY413: '605', CY414: '600', CY415: '601', CY411: '617', CY412: '618', CY416: '617', CY417: '618',
    CZ410: '605', CZ405: '600', CZ406: '601', CZ409: '604', CZ407: '602', CZ408: '603', CZ413: '605', CZ414: '600', CZ415: '601', CZ411: '617', CZ412: '618', CZ416: '617', CZ417: '618',
    DK410: '605', DK405: '600', DK406: '601', DK409: '604', DK407: '602', DK408: '603', DK413: '605', DK414: '600', DK415: '601', DK411: '617', DK412: '618', DK416: '617', DK417: '618',
    EE410: '605', EE405: '600', EE406: '601', EE409: '604', EE407: '602', EE408: '603', EE413: '605', EE414: '600', EE415: '601', EE411: '617', EE412: '618', EE416: '617', EE417: '618',
    FI410: '605', FI405: '600', FI406: '601', FI409: '604', FI407: '602', FI408: '603', FI413: '605', FI414: '600', FI415: '601', FI411: '617', FI412: '618', FI416: '617', FI417: '618',
    FR410: '605', FR405: '600', FR406: '601', FR409: '604', FR407: '602', FR408: '603', FR413: '605', FR414: '600', FR415: '601', FR411: '617', FR412: '618', FR416: '617', FR417: '618',
    DE410: '605', DE405: '600', DE406: '601', DE409: '604', DE407: '602', DE408: '603', DE413: '605', DE414: '600', DE415: '601', DE411: '617', DE412: '618', DE416: '617', DE417: '618',
    GR410: '605', GR405: '600', GR406: '601', GR409: '604', GR407: '602', GR408: '603', GR413: '605', GR414: '600', GR415: '601', GR411: '617', GR412: '618', GR416: '617', GR417: '618',
    HU410: '605', HU405: '600', HU406: '601', HU409: '604', HU407: '602', HU408: '603', HU413: '605', HU414: '600', HU415: '601', HU411: '617', HU412: '618', HU416: '617', HU417: '618',
    IE410: '605', IE405: '600', IE406: '601', IE409: '604', IE407: '602', IE408: '603', IE413: '605', IE414: '600', IE415: '601', IE411: '617', IE412: '618', IE416: '617', IE417: '618',
    IT410: '605', IT405: '600', IT406: '601', IT409: '604', IT407: '602', IT408: '603', IT413: '605', IT414: '600', IT415: '601', IT411: '617', IT412: '618', IT416: '617', IT417: '618',
    LV410: '605', LV405: '600', LV406: '601', LV409: '604', LV407: '602', LV408: '603', LV413: '605', LV414: '600', LV415: '601', LV411: '617', LV412: '618', LV416: '617', LV417: '618',
    LT410: '605', LT405: '600', LT406: '601', LT409: '604', LT407: '602', LT408: '603', LT413: '605', LT414: '600', LT415: '601', LT411: '617', LT412: '618', LT416: '617', LT417: '618',
    LU410: '605', LU405: '600', LU406: '601', LU409: '604', LU407: '602', LU408: '603', LU413: '605', LU414: '600', LU415: '601', LU411: '617', LU412: '618', LU416: '617', LU417: '618',
    MT410: '605', MT405: '600', MT406: '601', MT409: '604', MT407: '602', MT408: '603', MT413: '605', MT414: '600', MT415: '601', MT411: '617', MT412: '618', MT416: '617', MT417: '618',
    NO410: '605', NO405: '600', NO406: '601', NO409: '604', NO407: '602', NO408: '603', NO413: '605', NO414: '600', NO415: '601', NO411: '617', NO412: '618', NO416: '617', NO417: '618',
    PL410: '605', PL405: '600', PL406: '601', PL409: '604', PL407: '602', PL408: '603', PL413: '605', PL414: '600', PL415: '601', PL411: '617', PL412: '618', PL416: '617', PL417: '618',
    PT410: '605', PT405: '600', PT406: '601', PT409: '604', PT407: '602', PT408: '603', PT413: '605', PT414: '600', PT415: '601', PT411: '617', PT412: '618', PT416: '617', PT417: '618',
    RO410: '605', RO405: '600', RO406: '601', RO409: '604', RO407: '602', RO408: '603', RO413: '605', RO414: '600', RO415: '601', RO411: '617', RO412: '618', RO416: '617', RO417: '618',
    SK410: '605', SK405: '600', SK406: '601', SK409: '604', SK407: '602', SK408: '603', SK413: '605', SK414: '600', SK415: '601', SK411: '617', SK412: '618', SK416: '617', SK417: '618',
    SL410: '605', SL405: '600', SL406: '601', SL409: '604', SL407: '602', SL408: '603', SL413: '605', SL414: '600', SL415: '601', SL411: '617', SL412: '618', SL416: '617', SL417: '618',
    ES410: '605', ES405: '600', ES406: '601', ES409: '604', ES407: '602', ES408: '603', ES413: '605', ES414: '600', ES415: '601', ES411: '617', ES412: '618', ES416: '617', ES417: '618',
    SE410: '605', SE405: '600', SE406: '601', SE409: '604', SE407: '602', SE408: '603', SE413: '605', SE414: '600', SE415: '601', SE411: '617', SE412: '618', SE416: '617', SE417: '618',
    CH410: '605', CH405: '600', CH406: '601', CH409: '604', CH407: '602', CH408: '603', CH413: '605', CH414: '600', CH415: '601', CH411: '617', CH412: '618', CH416: '617', CH417: '618',
    NL410: '605', NL405: '600', NL406: '601', NL409: '604', NL407: '602', NL408: '603', NL413: '605', NL414: '600', NL415: '601', NL411: '617', NL412: '618', NL416: '617', NL417: '618',
    GB410: '605', GB405: '600', GB406: '601', GB409: '604', GB407: '602', GB408: '603', GB413: '605', GB414: '600', GB415: '601', GB411: '617', GB412: '618', GB416: '617', GB417: '618'
    };
    if ((countryCode == "US") || (countryCode == "CA") || (countryCode == "PH")) {
        location.href = "url.com?a=" + productCode + "&c=" + countryCode;
    }else{
        if ( (countryCode + productCode) in specialCodes) {
            productCode = specialCodes[countryCode + productCode];
            location.href = "urlnfr.com?a=" + productCode + "&c=" + countryCode;
        }

    }

}

</script>

修改

此更新的脚本有效。但是,如果您的方法更有效,请告诉我。我在这里做的只是采用specialCode数组并将其分成3个数组,一个用于nfr,一个用于eu,一个用于jp。

    function getCodes(){
var countryCode = document.getElementById("country").value;
var productCode = document.getElementById("products").value;

var euCodes = {
AT410: '605', AT405: '600', AT406: '601', AT409: '604', AT407: '602', AT408: '603', AT413: '605', AT414: '600', AT415: '601', AT411: '617', AT412: '618', AT416: '617', AT417: '618',
BE410: '605', BE405: '600', BE406: '601', BE409: '604', BE407: '602', BE408: '603', BE413: '605', BE414: '600', BE415: '601', BE411: '617', BE412: '618', BE416: '617', BE417: '618',
BG410: '605', BG405: '600', BG406: '601', BG409: '604', BG407: '602', BG408: '603', BG413: '605', BG414: '600', BG415: '601', BG411: '617', BG412: '618', BG416: '617', BG417: '618',
CY410: '605', CY405: '600', CY406: '601', CY409: '604', CY407: '602', CY408: '603', CY413: '605', CY414: '600', CY415: '601', CY411: '617', CY412: '618', CY416: '617', CY417: '618',
CZ410: '605', CZ405: '600', CZ406: '601', CZ409: '604', CZ407: '602', CZ408: '603', CZ413: '605', CZ414: '600', CZ415: '601', CZ411: '617', CZ412: '618', CZ416: '617', CZ417: '618',
DK410: '605', DK405: '600', DK406: '601', DK409: '604', DK407: '602', DK408: '603', DK413: '605', DK414: '600', DK415: '601', DK411: '617', DK412: '618', DK416: '617', DK417: '618',
EE410: '605', EE405: '600', EE406: '601', EE409: '604', EE407: '602', EE408: '603', EE413: '605', EE414: '600', EE415: '601', EE411: '617', EE412: '618', EE416: '617', EE417: '618',
FI410: '605', FI405: '600', FI406: '601', FI409: '604', FI407: '602', FI408: '603', FI413: '605', FI414: '600', FI415: '601', FI411: '617', FI412: '618', FI416: '617', FI417: '618',
FR410: '605', FR405: '600', FR406: '601', FR409: '604', FR407: '602', FR408: '603', FR413: '605', FR414: '600', FR415: '601', FR411: '617', FR412: '618', FR416: '617', FR417: '618',
DE410: '605', DE405: '600', DE406: '601', DE409: '604', DE407: '602', DE408: '603', DE413: '605', DE414: '600', DE415: '601', DE411: '617', DE412: '618', DE416: '617', DE417: '618',
GR410: '605', GR405: '600', GR406: '601', GR409: '604', GR407: '602', GR408: '603', GR413: '605', GR414: '600', GR415: '601', GR411: '617', GR412: '618', GR416: '617', GR417: '618',
HU410: '605', HU405: '600', HU406: '601', HU409: '604', HU407: '602', HU408: '603', HU413: '605', HU414: '600', HU415: '601', HU411: '617', HU412: '618', HU416: '617', HU417: '618',
IE410: '605', IE405: '600', IE406: '601', IE409: '604', IE407: '602', IE408: '603', IE413: '605', IE414: '600', IE415: '601', IE411: '617', IE412: '618', IE416: '617', IE417: '618',
IT410: '605', IT405: '600', IT406: '601', IT409: '604', IT407: '602', IT408: '603', IT413: '605', IT414: '600', IT415: '601', IT411: '617', IT412: '618', IT416: '617', IT417: '618',
LV410: '605', LV405: '600', LV406: '601', LV409: '604', LV407: '602', LV408: '603', LV413: '605', LV414: '600', LV415: '601', LV411: '617', LV412: '618', LV416: '617', LV417: '618',
LT410: '605', LT405: '600', LT406: '601', LT409: '604', LT407: '602', LT408: '603', LT413: '605', LT414: '600', LT415: '601', LT411: '617', LT412: '618', LT416: '617', LT417: '618',
LU410: '605', LU405: '600', LU406: '601', LU409: '604', LU407: '602', LU408: '603', LU413: '605', LU414: '600', LU415: '601', LU411: '617', LU412: '618', LU416: '617', LU417: '618',
MT410: '605', MT405: '600', MT406: '601', MT409: '604', MT407: '602', MT408: '603', MT413: '605', MT414: '600', MT415: '601', MT411: '617', MT412: '618', MT416: '617', MT417: '618',
NO410: '605', NO405: '600', NO406: '601', NO409: '604', NO407: '602', NO408: '603', NO413: '605', NO414: '600', NO415: '601', NO411: '617', NO412: '618', NO416: '617', NO417: '618',
PL410: '605', PL405: '600', PL406: '601', PL409: '604', PL407: '602', PL408: '603', PL413: '605', PL414: '600', PL415: '601', PL411: '617', PL412: '618', PL416: '617', PL417: '618',
PT410: '605', PT405: '600', PT406: '601', PT409: '604', PT407: '602', PT408: '603', PT413: '605', PT414: '600', PT415: '601', PT411: '617', PT412: '618', PT416: '617', PT417: '618',
RO410: '605', RO405: '600', RO406: '601', RO409: '604', RO407: '602', RO408: '603', RO413: '605', RO414: '600', RO415: '601', RO411: '617', RO412: '618', RO416: '617', RO417: '618',
SK410: '605', SK405: '600', SK406: '601', SK409: '604', SK407: '602', SK408: '603', SK413: '605', SK414: '600', SK415: '601', SK411: '617', SK412: '618', SK416: '617', SK417: '618',
SL410: '605', SL405: '600', SL406: '601', SL409: '604', SL407: '602', SL408: '603', SL413: '605', SL414: '600', SL415: '601', SL411: '617', SL412: '618', SL416: '617', SL417: '618',
ES410: '605', ES405: '600', ES406: '601', ES409: '604', ES407: '602', ES408: '603', ES413: '605', ES414: '600', ES415: '601', ES411: '617', ES412: '618', ES416: '617', ES417: '618',
SE410: '605', SE405: '600', SE406: '601', SE409: '604', SE407: '602', SE408: '603', SE413: '605', SE414: '600', SE415: '601', SE411: '617', SE412: '618', SE416: '617', SE417: '618',
CH410: '605', CH405: '600', CH406: '601', CH409: '604', CH407: '602', CH408: '603', CH413: '605', CH414: '600', CH415: '601', CH411: '617', CH412: '618', CH416: '617', CH417: '618',
NL410: '605', NL405: '600', NL406: '601', NL409: '604', NL407: '602', NL408: '603', NL413: '605', NL414: '600', NL415: '601', NL411: '617', NL412: '618', NL416: '617', NL417: '618',
GB410: '605', GB405: '600', GB406: '601', GB409: '604', GB407: '602', GB408: '603', GB413: '605', GB414: '600', GB415: '601', GB411: '617', GB412: '618', GB416: '617', GB417: '618'
};

var nfrCodes = {
    AU410: '485', AU405: '480', AU406: '481', AU409: '484', AU407: '482', AU408: '483', AU413: '492', AU414: '488', AU415: '489', AU411: '486', AU412: '487', AU416: '490', AU417: '491',
BS410: '485', BS405: '480', BS406: '481', BS409: '484', BS407: '482', BS408: '483', BS413: '492', BS414: '488', BS415: '489', BS411: '486', BS412: '487', BS416: '490', BS417: '491',
HK410: '485', HK405: '480', HK406: '481', HK409: '484', HK407: '482', HK408: '483', HK413: '492', HK414: '488', HK415: '489', HK411: '486', HK412: '487', HK416: '490', HK417: '491',
MX410: '485', MX405: '480', MX406: '481', MX409: '484', MX407: '482', MX408: '483', MX413: '492', MX414: '488', MX415: '489', MX411: '486', MX412: '487', MX416: '490', MX417: '491',
NZ410: '485', NZ405: '480', NZ406: '481', NZ409: '484', NZ407: '482', NZ408: '483', NZ413: '492', NZ414: '488', NZ415: '489', NZ411: '486', NZ412: '487', NZ416: '490', NZ417: '491',
SG410: '485', SG405: '480', SG406: '481', SG409: '484', SG407: '482', SG408: '483', SG413: '492', SG414: '488', SG415: '489', SG411: '486', SG412: '487', SG416: '490', SG417: '491',
TH410: '485', TH405: '480', TH406: '481', TH409: '484', TH407: '482', TH408: '483', TH413: '492', TH414: '488', TH415: '489', TH411: '486', TH412: '487', TH416: '490', TH417: '491'
};

var jpCodes = {
    JP410: '516', JP405: '516', JP406: '522', JP409: '517', JP407: '517', JP408: '523', JP413: '528', JP414: '528', JP415: '530', JP411: '521', JP412: '521', JP416: '534', JP417: '534'
};

if ((countryCode == "US") || (countryCode == "CA") || (countryCode == "PH")) {
    location.href = "url.com?a=" + productCode + "&c=" + countryCode;
}else{
    if ( (countryCode + productCode) in nfrCodes) {
        productCode = nfrCodes[countryCode + productCode];
    location.href = "urlnfr.com?a=" + productCode + "&c=" + countryCode;
    }else{
    if ( (countryCode + productCode) in euCodes) {
        productCode = euCodes[countryCode + productCode];
        location.href = "urleu.com?a=" + productCode + "&c=" + countryCode;
    }else{
        if ( (countryCode + productCode) in jpCodes) {
        productCode = jpCodes[countryCode + productCode];
        location.href = "urljp.com?a=" + productCode + "&c=" + countryCode + "&l=6";
        }
    }
    }
}
}

修改

更新了上面的脚本,将Else If语句组合为@wavemode建议。

更新后的脚本

    function getCodes(){
var countryCode = document.getElementById("country").value;
var productCode = document.getElementById("products").value;

var euCodes = {
AT410: '605', AT405: '600', AT406: '601', AT409: '604', AT407: '602', AT408: '603', AT413: '605', AT414: '600', AT415: '601', AT411: '617', AT412: '618', AT416: '617', AT417: '618',
BE410: '605', BE405: '600', BE406: '601', BE409: '604', BE407: '602', BE408: '603', BE413: '605', BE414: '600', BE415: '601', BE411: '617', BE412: '618', BE416: '617', BE417: '618',
BG410: '605', BG405: '600', BG406: '601', BG409: '604', BG407: '602', BG408: '603', BG413: '605', BG414: '600', BG415: '601', BG411: '617', BG412: '618', BG416: '617', BG417: '618',
CY410: '605', CY405: '600', CY406: '601', CY409: '604', CY407: '602', CY408: '603', CY413: '605', CY414: '600', CY415: '601', CY411: '617', CY412: '618', CY416: '617', CY417: '618',
CZ410: '605', CZ405: '600', CZ406: '601', CZ409: '604', CZ407: '602', CZ408: '603', CZ413: '605', CZ414: '600', CZ415: '601', CZ411: '617', CZ412: '618', CZ416: '617', CZ417: '618',
DK410: '605', DK405: '600', DK406: '601', DK409: '604', DK407: '602', DK408: '603', DK413: '605', DK414: '600', DK415: '601', DK411: '617', DK412: '618', DK416: '617', DK417: '618',
EE410: '605', EE405: '600', EE406: '601', EE409: '604', EE407: '602', EE408: '603', EE413: '605', EE414: '600', EE415: '601', EE411: '617', EE412: '618', EE416: '617', EE417: '618',
FI410: '605', FI405: '600', FI406: '601', FI409: '604', FI407: '602', FI408: '603', FI413: '605', FI414: '600', FI415: '601', FI411: '617', FI412: '618', FI416: '617', FI417: '618',
FR410: '605', FR405: '600', FR406: '601', FR409: '604', FR407: '602', FR408: '603', FR413: '605', FR414: '600', FR415: '601', FR411: '617', FR412: '618', FR416: '617', FR417: '618',
DE410: '605', DE405: '600', DE406: '601', DE409: '604', DE407: '602', DE408: '603', DE413: '605', DE414: '600', DE415: '601', DE411: '617', DE412: '618', DE416: '617', DE417: '618',
GR410: '605', GR405: '600', GR406: '601', GR409: '604', GR407: '602', GR408: '603', GR413: '605', GR414: '600', GR415: '601', GR411: '617', GR412: '618', GR416: '617', GR417: '618',
HU410: '605', HU405: '600', HU406: '601', HU409: '604', HU407: '602', HU408: '603', HU413: '605', HU414: '600', HU415: '601', HU411: '617', HU412: '618', HU416: '617', HU417: '618',
IE410: '605', IE405: '600', IE406: '601', IE409: '604', IE407: '602', IE408: '603', IE413: '605', IE414: '600', IE415: '601', IE411: '617', IE412: '618', IE416: '617', IE417: '618',
IT410: '605', IT405: '600', IT406: '601', IT409: '604', IT407: '602', IT408: '603', IT413: '605', IT414: '600', IT415: '601', IT411: '617', IT412: '618', IT416: '617', IT417: '618',
LV410: '605', LV405: '600', LV406: '601', LV409: '604', LV407: '602', LV408: '603', LV413: '605', LV414: '600', LV415: '601', LV411: '617', LV412: '618', LV416: '617', LV417: '618',
LT410: '605', LT405: '600', LT406: '601', LT409: '604', LT407: '602', LT408: '603', LT413: '605', LT414: '600', LT415: '601', LT411: '617', LT412: '618', LT416: '617', LT417: '618',
LU410: '605', LU405: '600', LU406: '601', LU409: '604', LU407: '602', LU408: '603', LU413: '605', LU414: '600', LU415: '601', LU411: '617', LU412: '618', LU416: '617', LU417: '618',
MT410: '605', MT405: '600', MT406: '601', MT409: '604', MT407: '602', MT408: '603', MT413: '605', MT414: '600', MT415: '601', MT411: '617', MT412: '618', MT416: '617', MT417: '618',
NO410: '605', NO405: '600', NO406: '601', NO409: '604', NO407: '602', NO408: '603', NO413: '605', NO414: '600', NO415: '601', NO411: '617', NO412: '618', NO416: '617', NO417: '618',
PL410: '605', PL405: '600', PL406: '601', PL409: '604', PL407: '602', PL408: '603', PL413: '605', PL414: '600', PL415: '601', PL411: '617', PL412: '618', PL416: '617', PL417: '618',
PT410: '605', PT405: '600', PT406: '601', PT409: '604', PT407: '602', PT408: '603', PT413: '605', PT414: '600', PT415: '601', PT411: '617', PT412: '618', PT416: '617', PT417: '618',
RO410: '605', RO405: '600', RO406: '601', RO409: '604', RO407: '602', RO408: '603', RO413: '605', RO414: '600', RO415: '601', RO411: '617', RO412: '618', RO416: '617', RO417: '618',
SK410: '605', SK405: '600', SK406: '601', SK409: '604', SK407: '602', SK408: '603', SK413: '605', SK414: '600', SK415: '601', SK411: '617', SK412: '618', SK416: '617', SK417: '618',
SL410: '605', SL405: '600', SL406: '601', SL409: '604', SL407: '602', SL408: '603', SL413: '605', SL414: '600', SL415: '601', SL411: '617', SL412: '618', SL416: '617', SL417: '618',
ES410: '605', ES405: '600', ES406: '601', ES409: '604', ES407: '602', ES408: '603', ES413: '605', ES414: '600', ES415: '601', ES411: '617', ES412: '618', ES416: '617', ES417: '618',
SE410: '605', SE405: '600', SE406: '601', SE409: '604', SE407: '602', SE408: '603', SE413: '605', SE414: '600', SE415: '601', SE411: '617', SE412: '618', SE416: '617', SE417: '618',
CH410: '605', CH405: '600', CH406: '601', CH409: '604', CH407: '602', CH408: '603', CH413: '605', CH414: '600', CH415: '601', CH411: '617', CH412: '618', CH416: '617', CH417: '618',
NL410: '605', NL405: '600', NL406: '601', NL409: '604', NL407: '602', NL408: '603', NL413: '605', NL414: '600', NL415: '601', NL411: '617', NL412: '618', NL416: '617', NL417: '618',
GB410: '605', GB405: '600', GB406: '601', GB409: '604', GB407: '602', GB408: '603', GB413: '605', GB414: '600', GB415: '601', GB411: '617', GB412: '618', GB416: '617', GB417: '618'
};

var nfrCodes = {
    AU410: '485', AU405: '480', AU406: '481', AU409: '484', AU407: '482', AU408: '483', AU413: '492', AU414: '488', AU415: '489', AU411: '486', AU412: '487', AU416: '490', AU417: '491',
BS410: '485', BS405: '480', BS406: '481', BS409: '484', BS407: '482', BS408: '483', BS413: '492', BS414: '488', BS415: '489', BS411: '486', BS412: '487', BS416: '490', BS417: '491',
HK410: '485', HK405: '480', HK406: '481', HK409: '484', HK407: '482', HK408: '483', HK413: '492', HK414: '488', HK415: '489', HK411: '486', HK412: '487', HK416: '490', HK417: '491',
MX410: '485', MX405: '480', MX406: '481', MX409: '484', MX407: '482', MX408: '483', MX413: '492', MX414: '488', MX415: '489', MX411: '486', MX412: '487', MX416: '490', MX417: '491',
NZ410: '485', NZ405: '480', NZ406: '481', NZ409: '484', NZ407: '482', NZ408: '483', NZ413: '492', NZ414: '488', NZ415: '489', NZ411: '486', NZ412: '487', NZ416: '490', NZ417: '491',
SG410: '485', SG405: '480', SG406: '481', SG409: '484', SG407: '482', SG408: '483', SG413: '492', SG414: '488', SG415: '489', SG411: '486', SG412: '487', SG416: '490', SG417: '491',
TH410: '485', TH405: '480', TH406: '481', TH409: '484', TH407: '482', TH408: '483', TH413: '492', TH414: '488', TH415: '489', TH411: '486', TH412: '487', TH416: '490', TH417: '491'
};

var jpCodes = {
    JP410: '516', JP405: '516', JP406: '522', JP409: '517', JP407: '517', JP408: '523', JP413: '528', JP414: '528', JP415: '530', JP411: '521', JP412: '521', JP416: '534', JP417: '534'
};

if ((countryCode == "US") || (countryCode == "CA") || (countryCode == "PH")) {
    location.href = "url.com?a=" + productCode + "&c=" + countryCode;
}else   if ( (countryCode + productCode) in nfrCodes) {
    productCode = nfrCodes[countryCode + productCode];
    location.href = "urlnfr.com?a=" + productCode + "&c=" + countryCode;
}else   if ( (countryCode + productCode) in euCodes) {
    productCode = euCodes[countryCode + productCode];
    location.href = "urleu.com?a=" + productCode + "&c=" + countryCode;
}else   if ( (countryCode + productCode) in jpCodes) {
    productCode = jpCodes[countryCode + productCode];
    location.href = "urljp.com?a=" + productCode + "&c=" + countryCode + "&l=6";
}
}

1 个答案:

答案 0 :(得分:0)

您可能需要手动将所有特殊组合输入到数组中,并检查如下:

/* store all special combinations here */
var specialCodes = {
    AU410: '485',
    BS410: '485',
    HK410: '485'
    /* etc */
};
/* check if the user's combination is a special case */
if ( (countryCode + productCode) in specialCodes) {
    productCode = specialCode[countryCode + productCode];
}

编辑:您可以使用类似的技术根据国家/地区指定特殊网址。按国家/地区创建包含特殊网址的对象:

/* put special url changes here */
var urls = {
    AU: "urlnrf.com",
    EE: "urleu.com",
    JP: "urljp.com"
    /* etc */
};

if ("US CA PH".indexOf(countryCode) >= 0) { 
    location.href = "url.com?a=" + productCode + "&c=" + countryCode;
}else{
    if ( (countryCode + productCode) in specialCodes) {
        productCode = specialCodes[countryCode + productCode];

        /* url is set to urls[countryCode] if it exists, otherwise "url.com" */
        location.href = (urls[countryCode] || "url.com") + "?a=" + productCode + "&c=" + countryCode;
    }

}