在jquery中使用trim

时间:2013-03-13 09:51:19

标签: 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?";
}

4 个答案:

答案 0 :(得分:3)

将其存储在变量中:

 var selectedValue = $.trim(selectedValue);

然后做:

if (selectedValue == "Oppty Locations") {

}

等...

答案 1 :(得分: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?";
}

答案 2 :(得分:1)

使用修剪后的值写入selectedValue并在之后使用,或者将其分配给某个变量并使用它。

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

答案 3 :(得分:0)

将其设为Switch语句而不是Else If Statement。

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