在ESNList文本字段中,需要用逗号分隔多个值,但它们必须在相同的范围内。例如,在循环中的下面条件中,如果在ESNList之间输入一个数字986329和999999,逗号后面的ESNList字段中的第二个输入应该与第一个输入在同一范围内,依此类推,否则我会抛出一个警告说两个ESN应该在同一范围内。我做了一个循环来执行检查,但它没有工作..有人可以告诉我错误在哪里..小提琴将有助于吨,
<html>
<head>
<script type="text/javascript" src="jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function () {
$(":text").css("border", "2px solid red");
$(":text").keyup(function(){
var enteredData = $(this).val()
console.log(enteredData);
if (enteredData == "") {
$(this).css("border", "2px solid red");
} else {
$(this).css("border", "inherit");
}
if ($(this).attr("id") == "ESNList"){
esnList = enteredData.split(',');
}
for(var i = 0; i < esnList.length; i++) {
if ( parseInt(esnList[i]) >= 986329 && parseInt(esnList[i]) <= 999999) {
$("#ddl_StxName").val("stx2");
$("#ddl_rtumodel").val("globalstar");
}
else if ( parseInt(esnList[i]) >= 660000 && parseInt(esnList[i]) <= 699999) {
$("#ddl_StxName").val("mmt");
$("#ddl_rtumodel").val("globalstar");
}
else if ( parseInt(esnList[i]) >= 200000 && parseInt(esnList[i]) <= 299999) {
$("#ddl_StxName").val("stm3");
$("#ddl_rtumodel").val("stmcomtech");
}
else if ( parseInt(esnList[i]) >= 1202114 && parseInt(esnList[i]) <= 1299999) {
$("#ddl_StxName").val("smartone");
$("#ddl_rtumodel").val("globalstar");
}
else { alert("ESNs should be within the same range"); }
}
});
});
</script> </head>
<body>
<form id="provision">
ESNList: <input type="text" id="ESNList" name="ESNList" size="30" /> <br />
ESN Start:<input type="text" id="ESNStart" name="ESNStart" size="10" /> <br />
ESN End: <input type="text" id="ESNStart" name="ESNStart" size="10" /> <br />
UnitName:<input type="text" id="STxName" name="STxName" size="30" /> <br />
Unit Model: <select name="STxName" id="ddl_StxName">
<option value="stx2">STX2</option>
<option value="stm3" selected>STM3</option>
<option value="acutec">Acutec</option>
<option value="trackpack">Trackpack</option>
<option value="mmt">MMT</option>
<option value="smartone">Smartone</option>
<option value="smartoneb" >SmartOneB</option>
</select> <br />
RTU Model Type:
<select name="rtumodel" id ="ddl_rtumodel">
<option value="globalstar">GlobalStar</option>
<option value="both">Both</option>
<option value="comtech">Comtech</option>
<option value="stmcomtech">STMComtech</option>
</select> <br />
<input type="submit" value ="submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
哦,我的,从哪里开始?
首先,每次连续的keyup事件后都会触发keyup,这意味着你要反复进行大量的比较,并获得很多&#34;否定&#34; ;在用户甚至完成第一个值之前。
为了节省一些痛苦,我开始使用你的代码,在这个小提琴:http://jsfiddle.net/mori57/68s68/
(请注意,我在底部放置了一个textarea作为输出,因此您可以确切地看到范围检查是如何发生的。我还注释了警报,因为它与keyup一起创建了一个非常讨厌的循环。)
问题是每个比较只存在于当前值的上下文中......它不能告诉&#34;开始范围&#34;应该是因为你没有存储任何这样的东西来进行比较,因此,当涉及到循环中的下一个值时,它只是测试它当前正在查看的项目是否在指定范围,而不是与最后一个查找项目相同的范围。
您可以通过将第一个比较存储在外部变量中,然后将第一个比较后的每个值与外部变量中保存的范围进行比较来实现。请参阅下面的代码以获取入门示例。另外,在上下文中看到它,这里: http://jsfiddle.net/mori57/68s68/2/
$(function () {
var out = $("#output");
var debug = function(txtIn){
out.val(out.val() + "\n" + txtIn);
};
// store your ranges here, in an array
var ranges = [
[986329,999999],
[660000,699999],
[200000,299999],
[1202114,1299999]
];
// store the activeRange that is set by the first item in the comparison loop
var activeRange = -1;
// use this to test the a value against a given range array
var withinRange = function(comparitor, range) {
return (comparitor >= range[0] && comparitor <= range[1]);
};
$(":text").css("border", "2px solid red");
// start the test onBlur, rather than on keyup
$(":text").blur(function () {
var enteredData = $(this).val()
console.log(enteredData);
if (enteredData == "") {
$(this).css("border", "2px solid red");
} else {
$(this).css("border", "inherit");
}
if ($(this).attr("id") == "ESNList") {
esnList = enteredData.split(',');
}
// loop through your values...
for (var i = 0; i < esnList.length; i++) {
// store the current value, rather than re-parseInt'ing it for every comparison
var intVal = parseInt(esnList[i]);
debug("intVal = " + intVal);
// now, find out if an activeRange has been set
if(activeRange == -1) {
// if not, we need to find out what the activeRange is
if (withinRange(intVal, ranges[0])) {
activeRange = ranges[0];
} else if (withinRange(intVal, ranges[1])) {
activeRange = ranges[1];
} else if (withinRange(intVal,ranges[2])) {
activeRange = ranges[2];
} else if (withinRange(intVal,ranges[3])) {
activeRange = ranges[3];
}
} else {
// if an activeRange has been set already,
// we need to find out if the current item is outside the activeRange
if (!withinRange(intVal, activeRange)) {
debug("Value out of active range.");
}
}
// Now that you know what your activeRange should be, you can
// decide what to set your drop down lists to reflect
switch (activeRange) {
case ranges[0]:
$("#ddl_StxName").val("stx2");
$("#ddl_rtumodel").val("globalstar");
break;
case ranges[1]:
$("#ddl_StxName").val("mmt");
$("#ddl_rtumodel").val("globalstar");
break;
case ranges[2]:
$("#ddl_StxName").val("stm3");
$("#ddl_rtumodel").val("stmcomtech");
break;
case ranges[3]:
$("#ddl_StxName").val("smartone");
$("#ddl_rtumodel").val("globalstar");
break;
default:
debug("Value out of range.");
break;
}
}
});
});