javascript - 根据下拉菜单和特定文本框访问字符串变量(具体取决于选择)

标签 javascript jquery

我正在尝试编写一个函数,该函数允许我基于下拉列表和文本框访问某个“管道”。这里的技巧是,下拉列表中的某些选项要么需要 ew(空重),要么需要 gvw(车辆总重),要么需要 ft(英尺),或者不需要。我设置的方式是,下拉列表的值可以与 var cars 中的字符串匹配,然后以某种方式需要知道如何根据用户在正确的相应文本框中输入的内容来访问正确的重量。任何对此的帮助将不胜感激!有人可以帮忙吗?

http://jsfiddle.net/wb9z2uuw/7/

** 也许类似于 var exampleWeightClass = Automobiles["2D"].vehicleClass; ????它如何知道属于正确的vehicleClass并知道它来自哪个文本框?

function getWeight(vehicle) { 
 if(vehicle == '2D' || vehicle == '3D' || vehicle == '4D' || vehicle == '5D' || vehicle == 'CV' || vehicle == 'UT' || vehicle == 'SW' || vehicle == 'TL1' || vehicle == 'TL2' || vehicle == 'PK' || vehicle == 'VN1' || vehicle == 'VN3' || vehicle == 'CA2' ||vehicle == 'PC' || vehicle == 'MH' ) { 
 return $('#ew').val();  //WEIGHT BASED ON EW
 } 
 else if(vehicle == 'TK1' || vehicle == 'TK2' || vehicle == 'TK3' || vehicle == 'TK4' || vehicle == 'TK5' ||  vehicle == 'VN2' ) { 
 return $('#gvw').val(); //WEIGHT BASED ON GVW 
 } 
 else if(vehicle == 'HS1' || vehicle == 'HS2' || vehicle == 'TV1' || vehicle == 'TV2' ) { 
 return $('#ft').val(); //WEIGHT BASED ON FT 
 } 
 else { 
 return 0; //CA1, HS3, PT1, PT2, MC1, MC2, MP
 }
}    

var Automobiles = {

    "2D": [{
        vehicleClass: "0 to 2499",
		minWeight: 0,
        maxWeight: 2500,
        pipe: [14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 29.00, 29.00, 29.00, 1, 27.60, 14.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "2500 to 3499",
		minWeight: 2500,
        maxWeight: 3500,
        pipe: [22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 1, 35.60, 22.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "3500 and up",
		minWeight: 3500,
        maxWeight: Number.MAX_VALUE,
        pipe: [32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 65.00, 65.00, 65.00, 1, 45.60, 32.50, 1, 3, 01, 01]
    }],

        
	"UT": [{
		vehicleClass: "0 to 2499",
		minWeight: 0,
        maxWeight: 2500,
        pipe: [14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 29.00, 29.00, 29.00, 1, 27.60, 14.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "2500 to 3499",
		minWeight: 2500,
        maxWeight: 3500,
        pipe: [22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 1, 35.60, 22.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "3500 and up",
		minWeight: 3500,
        maxWeight: Number.MAX_VALUE,
        pipe: [32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 65.00, 65.00, 65.00, 1, 45.60, 32.50, 1, 3, 01, 01]
    }],
 
	"SW": [{
		vehicleClass: "0 to 2499",
		minWeight: 0,
        maxWeight: 2500,
        pipe: [14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 29.00, 29.00, 29.00, 1, 27.60, 14.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "2500 to 3499",
		minWeight: 2500,
        maxWeight: 3500,
        pipe: [22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 1, 35.60, 22.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "3500 and up",
		minWeight: 3500,
        maxWeight: Number.MAX_VALUE,
        pipe: [32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 65.00, 65.00, 65.00, 1, 45.60, 32.50, 1, 3, 01, 01]
    }],
 
	"TL1": [{ 
		vehicleClass: "0 to 500",
		minWeight: 0,
        maxWeight: 501, 
		pipe: [6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 13.50, 13.50, 13.50, 2, 18.35, 6.75, 1, 1, 52, 52] 
	}],
 
	"TL2": [{
		vehicleClass: "501 and up",
		minWeight: 501,
        maxWeight: Number.MAX_VALUE, 
		pipe: [t, t, t, t, t, t, t, t, t, t, t, t, t, t, t, 1, t12, pt, 2, 2, 53, 53] 
	}],
 
	"PK": [{
		vehicleClass: "0 to 1999",
		minWeight: 0,
        maxWeight: 2000, 
		pipe: [14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 29.00, 29.00, 29.00, 1, 27.60, 14.50, 1, 3, 01, 01] 
	}, { 
		vehicleClass: "2000 to 3000",
		minWeight: 2000,
        maxWeight: 3001, 
		pipe: [22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 1, 35.60, 22.50, 1, 3, 01, 01] 
	}, { 
		vehicleClass: "3001 to 5000",
		minWeight: 3001,
        maxWeight: 5001, 
		pipe: [32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 65.00, 65.00, 65.00, 1, 45.60, 32.50, 1, 3, 01, 01] 
	}],
     
	"TK1": [{//pipe r22 has both 41 & 410
		vehicleClass: "5001 to 5999",
		minWeight: 5001,
        maxWeight: 6000,		
		pipe: [5.06, 10.13, 15.19, 20.25, 25.31, 30.38, 35.44, 40.50, 45.56, 50.63, 55.69, 60.75, 65.81, 70.88, 75.94, 1, 72.35, 60.75, 2, 3, 0, 41] 
	}, { 
		vehicleClass: "6000 to 7999",
		minWeight: 6000,
        maxWeight: 8000,
		pipe: [7.31, 14.63, 21.94, 29.25, 36.56, 43.88, 51.19, 58.50, 65.81, 73.13, 80.44, 87.75, 95.06, 102.38, 109.69, 1, 99.35, 87.75, 2, 3, 0, 41] 
	}, { 
		vehicleClass: "8000 to 9999",
		minWeight: 8000,
        maxWeight: 10000,
		pipe: [8.58, 17.17, 25.75, 34.33, 42.92, 51.50, 60.08, 68.67, 77.25, 85.83, 94.92, 103.00, 111.58, 120.17, 128.75, 1, 2, 103.00, 2, 1, 0, 41] 
	}, {
		vehicleClass: "10,000 to 14,999", 
		minWeight: 10000,
        maxWeight: 15000,
		pipe: [9.83, 19.67, 29.50, 39.33, 49.17, 59.00, 68.83, 78.67, 88.50, 98.33, 108.17, 118.00, 127.83, 137.67, 147.50, 1, 2, 118.00, 2, 1, 0, 410] 
	}, { 
		vehicleClass: "15,000 to 19,999",
		minWeight: 15000,
        maxWeight: 20000,
		pipe: [14.75, 29.50, 44.25, 59.00, 73.75, 88.50, 103.25, 118.00, 132.75, 147.50, 162.25, 177.00, 191.75, 206.50, 221.25, 1, 2, 177.00, 2, 1, 0, 410] 
	}, { 
		vehicleClass: "20,000 to 26,000",
		minWeight: 20000,
        maxWeight: 26001,
		pipe: [20.92, 41.83, 62.75, 83.67, 104.58, 125.50, 146.42, 167.33, 188.25, 209.17, 230.08, 251.00, 271.92, 292.83, 313.75, 1, 2, 251.00, 2, 1, 0, 410] 
	}, {
		vehicleClass: "26,001 to 34,999",
		minWeight: 26001,
        maxWeight: 35000,
		pipe: [27.00, 54.00, 81.00, 108.00, 135.00, 162.00, 189.00, 216.00, 243.00, 270.00, 297.00, 324.00, 351.00, 378.00, 405.00, 1, 2, 324.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "35,000 to 43,999", 
		minWeight: 35000,
        maxWeight: 44000,
		pipe: [33.75, 67.50, 101.25, 135.00, 168.75, 202.50, 236.25, 270.00, 303.75, 337.50, 371.25, 405.00, 438.75, 472.50, 506.25, 1, 2, 405.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "44,000 to 54,999", 
		minWeight: 44000,
        maxWeight: 55000,
		pipe: [64.42, 128.83, 193.25, 257.67, 322.08, 386.50, 450.92, 515.33, 579.75, 644.17, 708.58, 773.00, 837.42, 901.83, 966.25, 1, 2, 773.00, 2, 2, 0, 410] 
	}, {
		vehicleClass: "55,000 to 61,999", 
		minWeight: 55000,
        maxWeight: 62000,
		pipe: [76.33, 152.67, 229.00, 305.33, 381.67, 458.00, 534.33, 610.67, 687.00, 763.33, 839.67, 916.00, 992.33, 1068.67, 1145.00, 1, 2, 916.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "62,000 to 71,999", 
		minWeight: 62000,
        maxWeight: 72000,
		pipe: [90.00, 180.00, 270.00, 360.00, 450.00, 540.00, 630.00, 720.00, 810.00, 900.00, 990.00, 1080.00, 1170.00, 1260.00, 1350.00, 1, 2, 1080.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "72,000 to 80,000", 
		minWeight: 72000,
        maxWeight: 80001,
		pipe: [110.17, 220.33, 330.50, 440.67, 550.83, 661.00, 771.17, 881.33, 991.50, 1101.67, 1211.83, 1322.00, 1432.17, 1542.33, 1652.50, 1, 2, 1322.00, 2, 2, 0, 410] 
	}],
     
	"TK2": [{ 
		vehicleClass: "0 to 9999", 
		minWeight: 0,
        maxWeight: 10000,
		pipe: [7.31, 14.63, 21.94, 29.25, 36.56, 43.88, 51.19, 58.50, 65.81, 73.13, 80.44, 87.75, 95.06, 102.38, 109.69, 1, 2, 87.75, 2, 2, 0, 102] 
	}, { 
		vehicleClass: "10,000 thru 43,999", 
		minWeight: 10000,
        maxWeight: 44000,
		pipe: [7.31, 14.63, 21.94, 29.25, 36.56, 43.88, 51.19, 58.50, 65.81, 73.13, 80.44, 87.75, 95.06, 102.38, 109.69, 1, 2, 87.75, 2, 2, 0, 1020] 
	}, { 
		vehicleClass: "44000 to 80,000", 
		minWeight: 44000,
        maxWeight: 80001,
		pipe: [27.00, 54.00, 81.00, 108.00, 135.00, 162.00, 189.00, 216.00, 243.00, 270.00, 297.00, 324.00, 351.00, 378.00, 405.00, 1, 2, 324.00, 2, 2, 0, 1020] 
	}],
 
	"TK3": [{
		vehicleClass: "none", 
		pipe: [5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.63, 6.25, 6.88, 7.50, 8.13, 8.75, 9.38, 1, 2, 7.50, 2, 2, 0, 93] 
	}],
 
	"TK4": [{
		vehicleClass: "0 to 9999", 
		minWeight: 0,
        maxWeight: 10000,
		pipe: [27.00, 54.00, 81.00, 108.00, 135.00, 162.00, 189.00, 216.00, 243.00, 270.00, 297.00, 324.00, 351.00, 378.00, 405.00, 1, 2, 324.00, 2, 2, 0, 39] 
	}, { 
		vehicleClass: "10,000 and up", 
		minWeight: 10000,
        maxWeight: Number.MAX_VALUE,
		pipe: [27.00, 54.00, 81.00, 108.00, 135.00, 162.00, 189.00, 216.00, 243.00, 270.00, 297.00, 324.00, 351.00, 378.00, 405.00, 1, 2, 324.00, 2, 2, 0, 390] 
	}],
 
	"TK5": [{
		vehicleClass: "10,000 to 14,999", 
		minWeight: 10000,
        maxWeight: 15000,
		pipe: [9.83, 19.67, 29.50, 39.33, 49.17, 59.00, 68.83, 78.67, 88.50, 98.33, 108.17, 118.00, 127.83, 137.67, 147.50, 1, 2, 118.00, 2, 2, 0, 410]
	}, { 
		vehicleClass: "15,000 to 19,999", 
		minWeight: 15000,
        maxWeight: 20000,
		pipe: [14.75, 29.50, 44.25, 59.00, 73.75, 88.50, 103.25, 118.00, 132.75, 147.50, 162.25, 177.00, 191.75, 206.50, 221.25, 1, 2, 177.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "20,000 to 25,999", 
		minWeight: 20000,
        maxWeight: 26000,
		pipe: [20.92, 41.83, 62.75, 83.67, 104.58, 125.50, 146.42, 167.33, 188.25, 209.17, 230.08, 251.00, 271.92, 292.83, 313.75, 1, 2, 251.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "26,000 to 34,999", 
		minWeight: 26000,
        maxWeight: 35000,
		pipe: [27.00, 54.00, 81.00, 108.00, 135.00, 162.00, 189.00, 216.00, 243.00, 270.00, 297.00, 324.00, 351.00, 378.00, 405.00, 1, 2, 324.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "35,000 to 43,999", 
		minWeight: 35000,
        maxWeight: 44000,
		pipe: [33.75, 67.50, 101.25, 135.00, 168.75, 202.50, 236.25, 270.00, 303.75, 337.50, 371.25, 405.00, 438.75, 472.50, 506.25, 1, 2, 405.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "44,000 to 54,999", 
		minWeight: 44000,
        maxWeight: 55000,
		pipe: [64.33, 128.67, 193.00, 257.33, 321.67, 386.00, 450.33, 514.67, 579.00, 643.33, 707.67, 772.00, 836.33, 900.67, 965.00, 1, 2, 772.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "55,000 to 61,999", 
		minWeight: 55000,
        maxWeight: 62000,
		pipe: [76.25, 152.50, 228.75, 305.00, 381.25, 457.50, 533.75, 610.00, 686.25, 762.50, 838.75, 915.00, 991.25, 1067.50, 1143.75, 1, 2, 915.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "62,000 to 71,999", 
		minWeight: 62000,
        maxWeight: 72000,
		pipe: [90.00, 180.00, 270.00, 360.00, 450.00, 540.00, 630.00, 720.00, 810.00, 900.00, 990.00, 1080.00, 1170.00, 1260.00, 1350.00, 1, 2, 1080.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "72,000 to 80,000",	
		minWeight: 72000,
        maxWeight: 80001,
		pipe: [110.17, 220.33, 330.50, 440.67, 550.83, 661.00, 771.17, 881.33, 991.50, 1101.67, 1211.83, 1322.00, 1432.17, 1542.33, 1652.50, 1, 2, 1322.00, 2, 2, 0, 410] 
	}],
 
	"VN1": [{
		vehicleClass: "0 to 1999", 
		minWeight: 0,
        maxWeight: 2000,
		pipe: [14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 29.00, 29.00, 29.00, 1, 27.60, 14.50, 1, 3, 01, 01] 
	}, { 
		vehicleClass: "2000 to 3000", 
		minWeight: 2000,
        maxWeight: 3001,
		pipe: [22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 1, 35.60, 22.50, 1, 3, 01, 01] 
	}, { 
		vehicleClass: "3001 to 5000",
		minWeight: 3001,
        maxWeight: 5001,
		pipe: [32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 65.00, 65.00, 65.00, 1, 45.60, 32.50, 1, 3, 01, 01] 
	}],
 
	"VN2": [{//pipe r22 has both 41 & 410
		vehicleClass: "5001 to 5999",
		minWeight: 5001,
        maxWeight: 6000,		
		pipe: [5.06, 10.13, 15.19, 20.25, 25.31, 30.38, 35.44, 40.50, 45.56, 50.63, 55.69, 60.75, 65.81, 70.88, 75.94, 1, 72.35, 60.75, 2, 3, 0, 41] 
	}, { 
		vehicleClass: "6000 to 7999",
		minWeight: 6000,
        maxWeight: 8000,
		pipe: [7.31, 14.63, 21.94, 29.25, 36.56, 43.88, 51.19, 58.50, 65.81, 73.13, 80.44, 87.75, 95.06, 102.38, 109.69, 1, 99.35, 87.75, 2, 3, 0, 41] 
	}, { 
		vehicleClass: "8000 to 9999",
		minWeight: 8000,
        maxWeight: 10000,
		pipe: [8.58, 17.17, 25.75, 34.33, 42.92, 51.50, 60.08, 68.67, 77.25, 85.83, 94.92, 103.00, 111.58, 120.17, 128.75, 1, 2, 103.00, 2, 1, 0, 41] 
	}, {
		vehicleClass: "10,000 to 14,999", 
		minWeight: 10000,
        maxWeight: 15000,
		pipe: [9.83, 19.67, 29.50, 39.33, 49.17, 59.00, 68.83, 78.67, 88.50, 98.33, 108.17, 118.00, 127.83, 137.67, 147.50, 1, 2, 118.00, 2, 1, 0, 410] 
	}, { 
		vehicleClass: "15,000 to 19,999",
		minWeight: 15000,
        maxWeight: 20000,
		pipe: [14.75, 29.50, 44.25, 59.00, 73.75, 88.50, 103.25, 118.00, 132.75, 147.50, 162.25, 177.00, 191.75, 206.50, 221.25, 1, 2, 177.00, 2, 1, 0, 410] 
	}, { 
		vehicleClass: "20,000 to 26,000",
		minWeight: 20000,
        maxWeight: 26001,
		pipe: [20.92, 41.83, 62.75, 83.67, 104.58, 125.50, 146.42, 167.33, 188.25, 209.17, 230.08, 251.00, 271.92, 292.83, 313.75, 1, 2, 251.00, 2, 1, 0, 410] 
	}, {
		vehicleClass: "26,001 to 34,999",
		minWeight: 26001,
        maxWeight: 35000,
		pipe: [27.00, 54.00, 81.00, 108.00, 135.00, 162.00, 189.00, 216.00, 243.00, 270.00, 297.00, 324.00, 351.00, 378.00, 405.00, 1, 2, 324.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "35,000 to 43,999", 
		minWeight: 35000,
        maxWeight: 44000,
		pipe: [33.75, 67.50, 101.25, 135.00, 168.75, 202.50, 236.25, 270.00, 303.75, 337.50, 371.25, 405.00, 438.75, 472.50, 506.25, 1, 2, 405.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "44,000 to 54,999", 
		minWeight: 44000,
        maxWeight: 55000,
		pipe: [64.42, 128.83, 193.25, 257.67, 322.08, 386.50, 450.92, 515.33, 579.75, 644.17, 708.58, 773.00, 837.42, 901.83, 966.25, 1, 2, 773.00, 2, 2, 0, 410] 
	}, {
		vehicleClass: "55,000 to 61,999", 
		minWeight: 55000,
        maxWeight: 62000,
		pipe: [76.33, 152.67, 229.00, 305.33, 381.67, 458.00, 534.33, 610.67, 687.00, 763.33, 839.67, 916.00, 992.33, 1068.67, 1145.00, 1, 2, 916.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "62,000 to 71,999", 
		minWeight: 62000,
        maxWeight: 72000,
		pipe: [90.00, 180.00, 270.00, 360.00, 450.00, 540.00, 630.00, 720.00, 810.00, 900.00, 990.00, 1080.00, 1170.00, 1260.00, 1350.00, 1, 2, 1080.00, 2, 2, 0, 410] 
	}, { 
		vehicleClass: "72,000 to 80,000", 
		minWeight: 72000,
        maxWeight: 80001,
		pipe: [110.17, 220.33, 330.50, 440.67, 550.83, 661.00, 771.17, 881.33, 991.50, 1101.67, 1211.83, 1322.00, 1432.17, 1542.33, 1652.50, 1, 2, 1322.00, 2, 2, 0, 410] 
	}],
		
 
	"VN3": [{
		vehicleClass: "0 to 2499",
		minWeight: 0,
        maxWeight: 2500,
        pipe: [14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 14.50, 29.00, 29.00, 29.00, 1, 27.60, 14.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "2500 to 3499",
		minWeight: 2500,
        maxWeight: 3500,
        pipe: [22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 1, 35.60, 22.50, 1, 3, 01, 01]
    }, {
        vehicleClass: "3500 and up",
		minWeight: 3500,
        maxWeight: Number.MAX_VALUE,
        pipe: [32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 32.50, 65.00, 65.00, 65.00, 1, 45.60, 32.50, 1, 3, 01, 01]
    }],
 
	"CA1": [{
		vehicleClass: "none", 
		pipe: [13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 13.50, 27.00, 27.00, 27.00, 1, 25.10, 13.50, 2, 3, 62, 62] 
	}],
 
	"CA2": [{
		vehicleClass: "0 to 4499", 
		minWeight: 0,
        maxWeight: 4500,
		pipe: [27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 54.00, 54.00, 54.00, 1, 38.60, 27.00, 1, 3, 42, 42] 
	}, { 
		vehicleClass: "4500 and up", 
		minWeight: 4500,
        maxWeight: Number.MAX_VALUE,
		pipe: [47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 94.50, 94.50, 94.50, 1, 58.85, 47.25, 1, 3, 42, 42] 
	}],
   
	"HS1": [{//OUTSIDE
		vehicleClass: "0 to 35", 
		minWeight: 0,
        maxWeight: 36,
		pipe: [5.00, 5.00, 5.00, 10.00, 10.00, 10.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 25.00, 25.00, 25.00, 4, 25.10, 20.00, 2, 2, 0, 51] 
	}, { 
		vehicleClass: "36 to 40", 
		minWeight: 36,
        maxWeight: 41,
		pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 30.25, 4, 31.10, 25.00, 2, 2, 0, 51] 
	}, { 
		vehicleClass: "41 to 45", 
		minWeight: 41,
        maxWeight: 46,
		pipe: [7.50, 7.50, 7.50, 15.00, 15.00, 15.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 37.50, 37.50, 37.50, 4, 35.10, 30.00, 2, 2, 0, 51] 
	}, {
		vehicleClass: "46 to 50", 
		minWeight: 46,
        maxWeight: 51,
		pipe: [8.75, 8.75, 8.75, 17.50, 17.50, 17.50, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 43.75, 43.75, 43.75, 4, 40.10, 35.00, 2, 2, 0, 51]
	}, { 
		vehicleClass: "51 to 55", 
		minWeight: 51,
        maxWeight: 56,
		pipe: [10.00, 10.00, 10.00, 20.00, 20.00, 20.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 50.00, 50.00, 50.00, 4, 45.10, 40.00, 2, 2, 0, 51] 
	}, { 
		vehicleClass: "56 to 60", 
		minWeight: 56,
        maxWeight: 61,
		pipe: [11.25, 11.25, 11.25, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 56.25, 56.25, 56.25, 4, 50.10, 45.00, 2, 2, 0, 51] 
	}, { 
		vehicleClass: "61 to 65", 
		minWeight: 61,
        maxWeight: 66,
		pipe: [12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 62.50, 62.50, 62.50, 4, 55.10, 50.00, 2, 2, 0, 51] 
	}, { 
		vehicleClass: "66 and up", 
		minWeight: 66,
        maxWeight: Number.MAX_VALUE,
		pipe: [20.00, 20.00, 20.00, 40.00, 40.00, 40.00, 80.00, 80.00, 80.00, 80.00, 80.00, 80.00, 100.00, 100.00, 100.00, 4, 85.10, 80.00, 2, 2, 0, 51] 
	}],
 
     
	"HS2": [{//INSIDE
		vehicleClass: "0 to 35", 
		minWeight: 0,
        maxWeight: 36,
		pipe: [5.00, 5.00, 5.00, 10.00, 10.00, 10.00, 20.00, 20.00, 20.00, 20.00, 20.00, 20.00, 25.00, 25.00, 25.00, 4, 26.10, 20.00, 2, 2, 1, 510] 
	}, { 
		vehicleClass: "36 to 40", 
		minWeight: 36,
        maxWeight: 41,
		pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 31.25, 4, 31.10, 25.00, 2, 2, 1, 510]
	}, { 
		vehicleClass: "41 to 45", 
		minWeight: 41,
        maxWeight: 46,
		pipe: [7.50, 7.50, 7.50, 15.00, 15.00, 15.00, 30.00, 30.00, 30.00, 30.00, 30.00, 30.00, 37.50, 37.50, 37.50, 4, 36.10, 30.00, 2, 2, 1, 510] 
	}, {
		vehicleClass: "46 to 50", 
		minWeight: 46,
        maxWeight: 51,
		pipe: [8.75, 8.75, 8.75, 17.50, 17.50, 17.50, 35.00, 35.00, 35.00, 35.00, 35.00, 35.00, 43.75, 43.75, 43.75, 4, 41.10, 35.00, 2, 2, 1, 510]
	}, { 
		vehicleClass: "51 to 55", 
		minWeight: 51,
        maxWeight: 56,
		pipe: [10.00, 10.00, 10.00, 20.00, 20.00, 20.00, 40.00, 40.00, 40.00, 40.00, 40.00, 40.00, 50.00, 50.00, 50.00, 4, 46.10, 40.00, 2, 2, 1, 510] 
	}, { 
		vehicleClass: "56 to 60", 
		minWeight: 56,
        maxWeight: 61,
		pipe: [11.25, 11.25, 11.25, 22.50, 22.50, 22.50, 45.00, 45.00, 45.00, 45.00, 45.00, 45.00, 56.25, 56.25, 56.25, 4, 51.10, 45.00, 2, 2, 1, 510] 
	}, {
		vehicleClass: "61 to 65", 
		minWeight: 61,
        maxWeight: 66,
		pipe: [12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 50.00, 50.00, 50.00, 50.00, 50.00, 50.00, 62.50, 62.50, 62.50, 4, 56.10, 50.00, 2, 2, 1, 510] 
	}, { 
		vehicleClass: "66 and up", 
		minWeight: 66,
        maxWeight: Number.MAX_VALUE,
		pipe: [20.00, 20.00, 20.00, 40.00, 40.00, 40.00, 80.00, 80.00, 80.00, 80.00, 80.00, 80.00, 100.00, 100.00, 100.00, 4, 86.10, 80.00, 2, 2, 1, 510] 
	}],
 
	"HS3": [{
		vehicleClass: "none", 
		pipe: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4, 2, 2, 2, 2, 4, 86] 
	}],   

	"PC": [{
		vehicleClass: "0 to 4499", 
		minWeight: 0,
        maxWeight: 4500,
		pipe: [27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 54.00, 54.00, 54.00, 1, 38.60, 27.00, 2, 3, 42, 42] 
	}, { 
		vehicleClass: "4500 and up", 
		minWeight: 4500,
        maxWeight: Number.MAX_VALUE,
		pipe: [47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 94.50, 94.50, 94.50, 1, 58.85, 47.25, 2, 3, 42, 42] 
	}],
 
	"MH": [{
		vehicleClass: "0 to 4499", 
		minWeight: 0,
        maxWeight: 4500,
		pipe: [27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 27.00, 54.00, 54.00, 54.00, 1, 38.60, 27.00, 1, 3, 42, 42] 
	}, { 
		vehicleClass: "4500 and up", 
		minWeight: 4500,
        maxWeight: Number.MAX_VALUE,
		pipe: [47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 47.25, 94.50, 94.50, 94.50, 1, 58.85, 47.25, 1, 3, 42, 42]
	}],

	"PT1": [{
		vehicleClass: "none", 
		pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 31.25, 1, 36.60, 25.00, 2, 2, 0, 76] 
	}],
 
	"PT2": [{
		vehicleClass: "none", 
		pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 31.25, 1, 36.60, 25.00, 2, 2, 0, 76] 
	}],
 
	"TV1": [{
		vehicleClass: "0 to 35", //ft
		minWeight: 0,
        maxWeight: 36,
		pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 31.25, 1, 36.60, 25.00, 2, 2, 0, 76] 
	}],

	"TV2": [{
		vehicleClass: "36 and up", //ft
		minWeight: 36,
        maxWeight: Number.MAX_VALUE,
		pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 31.25, 1, 36.60, 25.00, 2, 2, 0, 78] 
	}],

	"MC1": [{
		vehicleClass: "none", 
		pipe: [5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00, 12.50, 12.50, 12.50, 1, 24.10, 10.00, 2, 1, 65, 65] 
	}],
 
	"MC2": [{
		vehicleClass: "none", 
		pipe: [5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00, 12.50, 12.50, 12.50, 1, 24.10, 10.00, 2, 1, 65, 65] 
	}],
 
	"MP": [{
		vehicleClass: "none", 
		pipe: [5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 6.25, 6.25, 6.25, 2, 19.10, 6.75, 2, 1, 69, 69] 
	}]
 };

   var weight = getWeight(vehiclebody.options[vehiclebody.selectedIndex].value);
    var vehiclebody = document.getElementById("vehiclebody");
    var eventHandler = function () {
        if (weight !== "" && vehiclebody.options[vehiclebody.selectedIndex].value != "") {
            for (var i in Automobiles) {
                if (i === vehiclebody.options[vehiclebody.selectedIndex].value) {
                    for (var a = 0; a < Automobiles[i].length; a++) {
                        if (weight >= Automobiles[i][a].minWeight && weight < Automobiles[i][a].maxWeight) {
                            alert(Automobiles[i][a].pipe);
                            break;
                        }
                    }
                    break;
                }
            }
        }
    };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select name="vehiclebody" id="vehiclebody">
<option value="">Choose a Vehicle</option>
    <option value="">Choose a Vehicle</option>
    <option value="2D">2-Door Coupe</option>
    <option value="UT">Sport Utility Vehicle</option>
    <option value="SW">Station Wagon</option>
    <option value="TL1">Trailers (utility or boat) under 500 lbs</option>
    <option value="TL2">Trailers (utility or boat) over 500 lbs</option>
	<option value="PK">Truck (Equal to or Less than 5000 lbs)</option>
    <option value="TK1">Truck (More than 5000 lbs)</option>
	<option value="TK2">Agricultural Truck</option>
	<option value="TK3">Agricultural Goat</option>
	<option value="TK4">Forestry Truck</option>
	<option value="TK5">Wrecker Truck</option>
    <option value="VN1">Van-Cargo (Equal to or Less than 5000 lbs)</option>
    <option value="VN2">Van-Cargo (More than 5000 lbs)</option>
    <option value="VN3">Van-Passenger</option>
    <option value="CA1">Camper (folding wall or popup)</option>
	<option value="CA2">Chassis Mount Camper (unit affixed to truck chassis)</option>
    <option value="HS1">Mobile Home Outside Mobile Home Park(rental property)</option>
	<option value="HS2">Mobile Home Inside Mobile Home Park(rental property)</option>
    <option value="HS3">Mobile Home (real property)</option>
    <option value="PC">Motor Coach (3 axles)</option>
    <option value="MH">Motor Home (2 axles)</option>
    <option value="PT1">Park Model (rental property)</option>
    <option value="PT2">Park Model (real property)</option>
    <option value="TV1">Travel Trailer</option>
    <option value="TV2">Travel Trailer (real property)</option>
    <option value="MC1">Motorcycle (More than 2bhp or 50cc)</option>
    <option value="MC2">Motorscooter (Equal to/Less than 2bhp/50cc)</option>
    <option value="MP">Moped (Equal to/Less than 2bhp/50cc)</option>
</select>
<br>
<label for="ew">Empty Weight:</label>
         <input type="text" name="ew" id="ew">
<br>
<label for="gvw">Gross Vehicle Weight:</label>
         <input type="text" name="gvw" id="gvw">
<br>             
<label for="ft">Feet:</label>
         <input type="text" name="ft" id="ft">

最佳答案

好的,我们开始吧:)。在这里查看它的实际情况:http://jsfiddle.net/wb9z2uuw/11/

我在代码中发现了一些问题。首先 - 在您的数据集中 - 每个对象都必须具有两个属性 minWeightmaxWeight。如果 minWeightmaxWeight 属性对于特定车辆并不重要,则将它们分别设置为 0 和 Number.MAX_VALUE。这可确保检索管道值的函数正常工作。例如:

"PT1": [{
    vehicleClass: "none",
    minWeight: 0,
    maxWeight: Number.MAX_VALUE,
    pipe: [6.25, 6.25, 6.25, 12.50, 12.50, 12.50, 25.00, 25.00, 25.00, 25.00, 25.00, 25.00, 31.25, 31.25, 31.25, 1, 36.60, 25.00, 2, 2, 0, 76]
}],

第二 - 在 eventHandler() 函数中,变量是在函数外部声明的,因此这些值保留为函数第一次运行时的样子,这就是代码不存在的原因根本不工作。

原文:

var weight = getWeight(vehiclebody.options[vehiclebody.selectedIndex].value);
var vehiclebody = document.getElementById("vehiclebody");
var eventHandler = function () {
    if (weight !== "" && vehiclebody.options[vehiclebody.selectedIndex].value != "") {
        for (var i in Automobiles) {
            if (i === vehiclebody.options[vehiclebody.selectedIndex].value) {
                for (var a = 0; a < Automobiles[i].length; a++) {
                    if (weight >= Automobiles[i][a].minWeight && weight < Automobiles[i][a].maxWeight) {
                        alert(Automobiles[i][a].pipe);
                        break;
                    }
                }
                break;
            }
        }
    }
};

新:

var eventHandler = function () {
    var weight = getWeight();
    var vehiclebody = $("#vehiclebody").val();
    if (weight !== "" && vehiclebody != "") {
        for (var i in Automobiles) {
            if (i === vehiclebody) {
                for (var a = 0; a < Automobiles[i].length; a++) {
                    if (weight >= Automobiles[i][a].minWeight && weight < Automobiles[i][a].maxWeight) {
                        alert(Automobiles[i][a].pipe);
                        break;
                    }
                }
                break;
            }
        }
    }
};

另外 - 只是为了帮助我稍微了解一下我应该输入哪个值,我在 getWeight() 函数中添加了一些动态隐藏和显示文本框的功能,您可以删除如果你想要这些

if (vehicle == '2D' || vehicle == '3D' || vehicle == '4D' || vehicle == '5D' || vehicle == 'CV' || vehicle == 'UT' || vehicle == 'SW' || vehicle == 'TL1' || vehicle == 'TL2' || vehicle == 'PK' || vehicle == 'VN1' || vehicle == 'VN3' || vehicle == 'CA2' || vehicle == 'PC' || vehicle == 'MH') {
    $("#gvw, label[for='gvw'], #ft, label[for='ft']").val("").hide();
    $("#ew, label[for='ew']").show();
    weight = $('#ew').val();  //WEIGHT BASED ON EW
}

这是完整的代码(减去数据集,StackOverflow 对答案的字符数限制为 30,000 个):

HTML:

<select name="vehiclebody" id="vehiclebody">
    <option value="">Choose a Vehicle</option>
    <option value="2D">2-Door Coupe</option>
    <option value="3D">3-Door Hatchback</option>
    <option value="4D">4-Door Sedan</option>
    <option value="5D">5-Door Hatchback</option>
    <option value="CV">Convertible</option>
    <option value="UT">Sport Utility Vehicle</option>
    <option value="SW">Station Wagon</option>
    <option value="TL1">Trailers (utility or boat) under 500 lbs</option>
    <option value="TL2">Trailers (utility or boat) over 500 lbs</option>
    <option value="PK">Truck (Equal to or Less than 5000 lbs)</option>
    <option value="TK1">Truck (More than 5000 lbs)</option>
    <option value="TK2">Agricultural Truck</option>
    <option value="TK3">Agricultural Goat</option>
    <option value="TK4">Forestry Truck</option>
    <option value="TK5">Wrecker Truck</option>
    <option value="VN1">Van-Cargo (Equal to or Less than 5000 lbs)</option>
    <option value="VN2">Van-Cargo (More than 5000 lbs)</option>
    <option value="VN3">Van-Passenger</option>
    <option value="CA1">Camper (folding wall or popup)</option>
    <option value="CA2">Chassis Mount Camper (unit affixed to truck chassis)</option>
    <option value="HS1">Mobile Home Outside Mobile Home Park(rental property)</option>
    <option value="HS2">Mobile Home Inside Mobile Home Park(rental property)</option>
    <option value="HS3">Mobile Home (real property)</option>
    <option value="PC">Motor Coach (3 axles)</option>
    <option value="MH">Motor Home (2 axles)</option>
    <option value="PT1">Park Model (rental property)</option>
    <option value="PT2">Park Model (real property)</option>
    <option value="TV1">Travel Trailer</option>
    <option value="TV2">Travel Trailer (real property)</option>
    <option value="MC1">Motorcycle (More than 2bhp or 50cc)</option>
    <option value="MC2">Motorscooter (Equal to/Less than 2bhp/50cc)</option>
    <option value="MP">Moped (Equal to/Less than 2bhp/50cc)</option>
</select>

<label for="ew">Empty Weight:</label>
<input type="text" name="ew" id="ew" />

<label for="gvw">Gross Vehicle Weight:</label>
<input type="text" name="gvw" id="gvw" />

<label for="ft">Feet:</label>
<input type="text" name="ft" id="ft" />

数据集:

由于 StackOverflow 的答案长度为 30,000 个字符,我不得不删除数据集 - 请参阅 fiddle 以获取完整的数据集

JavaScript:

$("#ew, label[for='ew'], #gvw, label[for='gvw'], #ft, label[for='ft']").hide();
function getWeight() {
    var weight = 0;
    var vehicle = $("#vehiclebody").val();
    if (vehicle == '2D' || vehicle == '3D' || vehicle == '4D' || vehicle == '5D' || vehicle == 'CV' || vehicle == 'UT' || vehicle == 'SW' || vehicle == 'TL1' || vehicle == 'TL2' || vehicle == 'PK' || vehicle == 'VN1' || vehicle == 'VN3' || vehicle == 'CA2' || vehicle == 'PC' || vehicle == 'MH') {
        $("#gvw, label[for='gvw'], #ft, label[for='ft']").val("").hide();
        $("#ew, label[for='ew']").show();
        weight = $('#ew').val();  //WEIGHT BASED ON EW
    }
    else if (vehicle == 'TK1' || vehicle == 'TK2' || vehicle == 'TK3' || vehicle == 'TK4' || vehicle == 'TK5' || vehicle == 'VN2') {
        $("#ew, label[for='ew'], #ft, label[for='ft']").val("").hide();
        $("#gvw, label[for='gvw']").show();
        weight = $('#gvw').val(); //WEIGHT BASED ON GVW 
    }
    else if (vehicle == 'HS1' || vehicle == 'HS2' || vehicle == 'TV1' || vehicle == 'TV2') {
        $("#ew, label[for='ew'], #gvw, label[for='gvw']").val("").hide();
        $("#ft, label[for='ft']").show();
        weight = $('#ft').val(); //WEIGHT BASED ON FT 
    }
    else {
        $("#ew, label[for='ew'], #gvw, label[for='gvw'], #ft, label[for='ft']").val("").hide();
        weight = 0; //CA1, HS3, PT1, PT2, MC1, MC2, MP
    }
    return weight;
}
var eventHandler = function () {
    var weight = getWeight();
    var vehiclebody = $("#vehiclebody").val();
    if (weight !== "" && vehiclebody != "") {
        for (var i in Automobiles) {
            if (i === vehiclebody) {
                for (var a = 0; a < Automobiles[i].length; a++) {
                    if (weight >= Automobiles[i][a].minWeight && weight < Automobiles[i][a].maxWeight) {
                        alert(Automobiles[i][a].pipe);
                        break;
                    }
                }
                break;
            }
        }
    }
};
$("#ew, #gvw, #ft, #vehiclebody").change(function () { eventHandler() });

关于javascript - 根据下拉菜单和特定文本框访问字符串变量(具体取决于选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921407/

相关文章:

javascript - jQuery .height() 没有按预期工作

javascript - iframe 自动调整其高度以适应内容高度

javascript - 为 Mongodb 创建多个动态对象 NodeJs 查询

javascript - 如何更改函数以接受不同类型作为参数

javascript - 在 div 元素中调用 JQuery 函数

c# - ASP.NET : Client-side validation, 服务器端操作

javascript - 根据日期输入重定向?

javascript - 使用 Yii 2 中的 js 函数将 textInput 值添加到另一个文本输入

javascript - 对象,参数,jquery

c# - 包含逗号和数字的正则表达式