不能使用jQuery添加两个十进制数字

sam*_*shi 11 javascript jquery decimal

我试图添加两个十进制值,但返回的总和是纯整数.怎么了?我找不到它.欢迎任何帮助.

jQuery(".delivery-method #ship_select").change(function(){
    var cost = jQuery(this).val(); 
    jQuery("#delivery_cost").val(cost); //returns 20.00
    var tot = parseInt(cost) + parseInt(total); //total returns 71.96
});
Run Code Online (Sandbox Code Playgroud)

使用代码我只得到91而不是91.96

小智 31

parseFloat()而不是parseInt().

jQuery(".delivery-method #ship_select").change(function(){
    var cost = jQuery(this).val(); 
    jQuery("#delivery_cost").val(cost); //returns 20.00
    var tot = parseFloat(cost) + parseFloat(total); //total returns 71.96
});
Run Code Online (Sandbox Code Playgroud)


Jig*_*put 7

你必须使用parseFloat而不是parseInt

jQuery(".delivery-method #ship_select").change(function(){
      var cost = jQuery(this).val(); 
      jQuery("#delivery_cost").val(cost); //returns 20.00
     var tot = parseFloat(cost) + parseFloat(total); //total returns 71.96
 });
Run Code Online (Sandbox Code Playgroud)

检查演示:http: //jsfiddle.net/aDYhX/1/