下面有五个文本栏,我要做的工作就是检查五个文本栏中的值不能相同。
1[1] | 2[1] | 3[1] | 4[1] | 5[1] |
1[2] | 2[2] | 3[2] | 4[2] | 5[2] |
请问有没有好一点的比较法。
要求用JS编写。
我的检查方法是如下:
//1与2
if (1 == 2) {
window.alert("1与2不能一样!!");
return false;
}
//1与3
if (1 == 3) {
window.alert("1与3不能一样!!");
return false;
}
//1与4
if (1 == 4) {
window.alert("1与4不能一样!!");
return false;
}
//1与5
if (1 == 5) {
window.alert("1与5不能一样!!");
return false;
}
//2与3
if (2 == 3) {
window.alert("2与3不能一样!!");
return false;
}
//2与4
if (2 == 4) {
window.alert("2与4不能一样!!");
return false;
}
//2与5
if (2 == 5) {
window.alert("2与5不能一样!!");
return false;
}
//3与4
if (3 == 4) {
window.alert("3与4不能一样!!");
return false;
}
//3与5
if (3 == 5) {
window.alert("3与5不能一样!!");
return false;
}
//4与5
if (4 == 5) {
window.alert("4与5不能一样!!");
return false;
}
====================================================
修正之后的算法
<html>
<script>
function a(){
len = 5;
for(i=0;i<len-1;i++){
for(j=i+1;j<len;j++){
first= eval("document.ff.t"+i);
second = eval("document.ff.t"+j);
if(first.value!=""&&second.value!=""&&second.value==first.value){
k=i+1;h=j+1;alert("第"+k+"项不能和第"+h+"项相等")}
}
}
}
</script>
<body>
<form name="ff">
<input type="text" name="t0">
<input type="text" name="t1">
<input type="text" name="t2">
<input type="text" name="t3">
<input type="text" name="t4">
<input type="submit" onclick="a();">
</form>
</body>
</html>