[英]Help with comparing to array values in JavaScript
我已經在您的幫助下開發了此代碼,有關stackoverflow。 我添加了一個額外的部分,它比較了兩個不同數組中的兩個數字,在本例中為offhire1和pro2。 問題出在我的代碼中:
(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;
}
我不太確定我會追蹤您。 如果數字相同,則該語句將不匹配。
代碼中的一個問題是您正在比較字符串,而不是數字。 您可能需要將其更改為:
(parseInt(offhire1[i].value) > parseInt(pro2[i].value))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.