2013-03-13 48 views
0

,而不是使用修剪所有我想用一次作爲共同爲下面的代碼的時候...請幫助我jQuery中使用裝飾

if ("Company Profile" == $.trim(selectedValue)) { 
    actionClass = "locationImportCompany.do?"; 
} else if ("Oppty Locations" == $.trim(selectedValue)) { 
    actionClass = "locationImport.do?"; 
} else if ("Different Oppty" == $.trim(selectedValue)) { 
    actionClass = "locationImportDiffOppty.do?"; 
} else if ("Loop TrackId Profile" == $.trim(selectedValue)) { 
    actionClass = "locationImportLoopTrkId.do?"; 
} else if ("Inventory" == $.trim(selectedValue)) { 
    isOrdering = "Y"; 
    actionClass = "inventoryLaunch.do?"; 
} else if ("Data Center List" == $.trim(selectedValue)) { 
    actionClass = "locationImportDataCenter.do?"; 
} else if ("Customer AccountId" == $.trim(selectedValue)) { 
    actionClass = "inventoryLaunch.do?"; 
} else if ("Customer Name" == $.trim(selectedValue)) { 
    actionClass = "locationImportDataCenter.do?"; 
} 
+0

其中是代碼???? – 2013-03-13 09:53:07

回答

1

在寫了selectedValue與修整值,之後使用,或者將其指定到一些變量並使用它。

selectedValueTrimmed = $.trim(selectedValue) 
if("Company Profile"==selectedValueTrimmed){ 
3

儲存於一個變量:

var selectedValue = $.trim(selectedValue); 

然後做:

if (selectedValue == "Oppty Locations") { 

} 

等等

+0

PS所有這些如果的,但如果使用的查找對象1可能被替換。 – QuentinUK 2013-03-13 10:11:17

2
var selectedValue = $.trim(selectedValue); 
if ("Company Profile" == selectedValue) { 
    actionClass = "locationImportCompany.do?"; 
} else if ("Oppty Locations" == selectedValue) { 
    actionClass = "locationImport.do?"; 
} else if ("Different Oppty" == selectedValue) { 
    actionClass = "locationImportDiffOppty.do?"; 
} else if ("Loop TrackId Profile" == selectedValue) { 
    actionClass = "locationImportLoopTrkId.do?"; 
} else if ("Inventory" == selectedValue) { 
    isOrdering = "Y"; 
    actionClass = "inventoryLaunch.do?"; 
} else if ("Data Center List" == selectedValue) { 
    actionClass = "locationImportDataCenter.do?"; 
} else if ("Customer AccountId" == selectedValue) { 
    actionClass = "inventoryLaunch.do?"; 
} else if ("Customer Name" == selectedValue) { 
    actionClass = "locationImportDataCenter.do?"; 
} 
0

使其代替switch語句中否則如果聲明。

switch ($.trim(selectedValue)) { 
    case: "Company Profile" 
     actionClass = "locationImportCompany.do?"; 
     break; 
    case: "Oppty Locations" 
     actionClass = "locationImport.do?"; 
     break; 
    // So on ... 
}