2011-03-21 62 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