帮助比较JavaScript中的数组值
我在stackoverflow的帮助下开发了这段代码。我在其中添加了一个额外的部分,用于比较来自两个不同数组的两个数字,在本例中是fire1和pro2。 问题出在我的代码中,我有:帮助比较JavaScript中的数组值,javascript,validation,Javascript,Validation,我在stackoverflow的帮助下开发了这段代码。我在其中添加了一个额外的部分,用于比较来自两个不同数组的两个数字,在本例中是fire1和pro2。 问题出在我的代码中,我有: (offhire1[i].value > pro2[i].value) 它只允许我在数字匹配时继续,即100=100。但我要做的是识别任何大于120>100的数值。我已经测试了这些值是否被传递,它们是否被传递。 我犯了什么错?谁能帮我解决呢 function validateoffhire(form) {
(offhire1[i].value > pro2[i].value)
它只允许我在数字匹配时继续,即100=100。但我要做的是识别任何大于120>100的数值。我已经测试了这些值是否被传递,它们是否被传递。
我犯了什么错?谁能帮我解决呢
function validateoffhire(form) {
var num1 = document.getElementById('num1');
var test2Regex = /^[0-9 ]+(([\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
var accumulator = 0;
var num2 = num1.value;
var i=0;
var offhire1 = [];
var pro2 =[];
for(var i = 0; i < num2; i++) {
offhire1[i] = document.getElementById('offhire1' + i);
pro2[i] = document.getElementById('pro2' + i);
var offhire2 = offhire1[i].value;
// var pro3 = pro2[i].value;
if(!offhire2.match(test2Regex)){
inlineMsg('offhire1' + i,'This needs to be an integer',10);
return false;
}
else if (offhire1[i].value > pro2[i].value) {
alert("You entered: " + pro2[i].value)
inlineMsg('offhire1' + i,'You have off hired to many items',10);
return false;
}
else{
accumulator += parseInt(offhire2);
}
}
if(accumulator <= 0){
inlineMsg('num1' ,'You have not off Hired any items',10);
return false;
}
return true;
}
函数validateoffhire(表单){
var num1=document.getElementById('num1');
var test2Regex=/^[0-9]+(([\,\.\-][a-zA-Z])?[a-zA-Z]*)*$/;
var累加器=0;
var num2=num1.value;
var i=0;
第1个风险值=[];
var pro2=[];
对于(变量i=0;ipro2[i].value){
警报(“您输入的:”+pro2[i].值)
inlineMsg('offhire1'+i,'youhaveoffhiredtomanytheitems',10);
返回false;
}
否则{
累加器+=parseInt(offfire2);
}
}
如果(累加器我不太确定我是否明白你的意思。如果数字相同,则语句不匹配
代码中的一个问题是比较字符串,而不是数字。您可能希望将其更改为:
(parseInt(offhire1[i].value) > parseInt(pro2[i].value))
谢谢你。这是我的错!