-1

我有以下值需要在 javascript 中进行硬编码和比较

值为:

SR = PlantWRShippingMail@shawinc.com
26 = Plant26ShippingMail@shawinc.com
29 = Plant29ShippingMail@shawinc.com
30 = Plant30ShippingMail@shawinc.com
34 = Plant34ShippingMail@shawinc.com
54 = Plant54ShippingMail@shawinc.com
41 = Plant41ShippingMail@shawinc.com
47 = Plant47ShippingMail@shawinc.com
DL = Plant.DL.Shipping.Mail@shawinc.com
37 = Plant37ShippingMail@shawinc.com
MU = PlantMUShippingMail@shawinc.com
UC = PlantUCShippingMail@shawinc.com
NB = plantnbshippingmail@shawinc.com

等等

我必须将该值与现有值进行比较,例如

var plantNo=document.getElementById('plantNo').value;
if(plantNo=`anyonevaluefromAbove`){
    //than get the value of it.
}

怎么做我在javascript中做到这一点。

4

3 回答 3

2

您可以简单地使用 JS 对象

var obj = {
SR: "PlantWRShippingMail@shawinc.com", ...
}

然后简单地看

if(obj[plantNo]) {
// obj[plantNo] is value 
}

这是你想要的吗?

于 2013-01-15T13:51:03.287 回答
2

您需要使用对象将硬编码值存储为地图;首先将所有植物存储在一个对象中:

var plants = {
  SR: "PlantWRShippingMail@shawinc.com",
  26: "Plant26ShippingMail@shawinc.com",
  29: "Plant29ShippingMail@shawinc.com",
  // and so on
  NB: "plantnbshippingmail@shawinc.com"
};

然后使用以下方法从对象中查找值:

var plantNo=document.getElementById('plantNo').value;
if(plants[plantNo]){
  alert(plants[plantNo]);
}
于 2013-01-15T13:51:41.960 回答
0

您可以将这些值存储在 javascript 对象中。

var someobj = {'SR' : 'PlantWRShippingMail@shawinc.com'
               // other keys
};

var plantNo=document.getElementById('plantNo').value;
if(someobj[plantNo]){
    alert(someobj[plantNo]); // than get the value of it.
}
于 2013-01-15T14:04:42.980 回答