2012-03-02 4 views
1

변수 개수가 0으로 설정되었습니다..text()에서 JQuery를 추가했습니다.

var count = 0; 

그런 다음 .each() 루프를 실행하여 개수를 업데이트합니다. .text() 매개 변수는 항상 텍스트 번호를 반환합니다. 내가 경고를 쳤을 때 내가 실수로는 .text()를 설정하려면 어떻게해야

$(this).find('.outersubtopic').each(function(idx) 
{ 
    var temp = $(this).text(); 

    count += temp;       
}); 

alert(count); 

는 항상 0008 대신에 단지 8

읽어?

답변

3
$(this).find('.outersubtopic').each(function(idx) 
{ 
    var temp = $(this).text(); 

    count += window.parseInt(temp);       
}); 

alert(count); 

window.parseInt()가이를 수행해야합니다.

2

당신은,

$(this).find('.outersubtopic').each(function(idx) 
{ 
    var temp = $(this).text(); 

    count += Number(temp);       
}); 

alert(count); 
+0

당신이 사용하는 경우 '에서는 parseInt를 사용하려고 번호로 변환해야 10)','parseInt ('015') === 13 '와 같이 원하지 않는 값으로 변환 할 수 있습니다. 또한'+ '단항 연산자를 사용하여 정수 값을 얻을 수 있습니다. 'count + = + temp' –

1

카운트 (()`당신은 항상 기수`에서는 parseInt를 추가해야 parseInt

$(this).find('.outersubtopic').each(function(idx) 
    { 
     var temp = $(this).text(); 

     count += temp;       
    }); 

    alert(parseInt(count)); 
0
count += 1 * temp; // quick way to change string to number 
관련 문제