2012-08-15 59 views
2

使用JQuery,我試圖設置div的id值的百分比並將其寫入另一個div。JQuery將Div值的百分比添加到另一個

這裏是我當前的代碼...

$("#percentage").text(
    parseFloat($("#total").text(percentage calculation here?)) 
    ); 

所以,如果#capacity有值100和#合計有10個數值則#percentage的價值將是10

我怎樣才能得到這個結果?

希望這是有道理

+0

當你說'div的ID value'你的意思''

20
='20'? – 2012-08-15 17:53:29

+0

是的,這是正確的 – Satch3000 2012-08-15 17:54:44

回答

5
$("#percentage").text(function() { 
    var result = (parseInt($("#capacity").text(), 10)/parseInt($("#total").text(), 10)); 
    if (!isFinite(result)) result = 0; 
    return result; 
});​ 

FIDDLE

+0

太快了。我幾乎沒有時間去第二parseInt。 – Ohgodwhy 2012-08-15 17:54:30

+0

雖然這會導致一個例外,如果total' ='0' – 2012-08-15 17:56:45

+0

@LayTaylor - 應該考慮無窮大和超越現在? – adeneo 2012-08-15 18:10:38

相關問題