2011-03-21 75 views
0

我已經開發了這個代碼,來自你們在這裏的幫助,在stackoverflow上。我在其中添加了一個額外的部分,它比較了兩個不同數組中的兩個數字,在本例中爲offhire1和pro2。 問題是在我的代碼,我有:幫助比較JavaScript中的數組值

(offhire1[i].value > pro2[i].value) 

它只允許我contine如果數字匹配即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; 
} 

回答

1

我不太清楚我跟着你。如果數字相同,則說明不匹配。

你的代碼中的一個問題是你比較字符串,而不是數字。您可能想要將其更改爲:

(parseInt(offhire1[i].value) > parseInt(pro2[i].value)) 
+0

謝謝你的cwolves。多數民衆贊成我的錯誤doh! – 2011-03-21 21:18:14