2013-04-04 72 views
1

這裏是我試圖計算的jsfiddle。我不知道如何將這些字符串轉換爲整數。 http://jsfiddle.net/Ygsrv/2/jquery將數字視爲字符串

這裏是代碼:

$('#get').click(function(){ 
    var priceProduct = 18.9 
    var length = $("#awp_group_5 option:selected").text().replace(/cm$/, ''); 
    var width = $("#awp_group_4 option:selected").text().replace(/cm$/, ''); 
    var priceProduct2 = priceProduct + (width + length * 30); 
    alert(priceProduct2); 

}); 

回答

4

您應該使用parseInt(yourStringHere);

你應該做的事情如下

var priceProduct2 = priceProduct + (parseInt(width) + parseInt(length) * 30); 

退房這個例子中,在小提琴http://jsfiddle.net/mayooresan/Ygsrv/3/

0

使用它來解析字符串爲Integer-

parseInt(YourStringVar);