我试图在第一个语句的第二个语句中重用相同的变量,if
我if
收到警告说我的变量是undefined
当我对其发出警报时。
我怎样才能让这个变量在第二条if
语句中保留它的值?我只是复制部分代码让你看看我在说什么。我试图foundRangeStart
在第二个 if 语句中使用 for ESNEnd
,它以前在第一个if
语句中使用过 for ESNStart
。
为什么在第二个语句中foundRangeStart
设置为?undefined
if
if ($(this).attr("id") == "ESNStart") {
var esnStart = [];
esnStart[0] = esn_to_num(enteredData);
var item;
var rangesFoundStart = [];
rangesFoundStart[0] = -1; // Set defaut value as -1 for the corresponding entry in the "rangesFound" array
for (var x in ranges) { // Inner loop over items in ranges object
item = esnStart[0];
if (item >= ranges[x].min && item <= ranges[x].max) {
rangesFoundStart[0] = x;
}
}
if (rangesFoundStart[0] >= 0) {
foundRangeStart = rangesFoundStart[0];
}
if (foundRangeStart && foundRangeStart >= 0) {
$("#STxModel").val(ranges[foundRangeStart].StxName);
$("#TxSpacing").val(ranges[foundRangeStart].rtumodel);
}
}
if ($(this).attr("id") == "ESNEnd") {
var esnEnd = [];
esnEnd[0] = esn_to_num(enteredData);
var item;
var foundRangeEnd;
var rangesFound = [];
rangesFound[0] = -1; // Set defaut value as -1 for the corresponding entry in the rangesFound array
for (var x in ranges) { // Inner loop over items in ranges object
item = esnEnd[0];
if (item >= ranges[x].min && item <= ranges[x].max) {
if (rangesFound[x] == foundRangeStart) {
rangesFound[0] = x;
var foundone = rangesFound[0] = x;
alert(foundone + foundRangeStart);
}
} else {
alert(alert_total);
}
}
}