2014-01-27 49 views
0

有一个函数,我想显示在div中的总数,它不工作。有任何想法吗?模糊显示在字段总数

基本上,在blur模式下,将vehiclePrice中的输入值乘以10%(0.010),然后在estimatedTaxesAndFees字段中将该总数显示为两个十进制固定量。

HTML:

<label for="vehiclePrice" class="form-control-label vpl">Vehicle Price</label> 
<input type="number" class="form-control" id="vehiclePrice" placeholder="$0" onkeypress="return isNumberKey(event)" onBlur="addCommas(this)" value="28435" /> 
<label for="estimatedTaxesAndFees" class="form-control-label etfl">Estimated Taxes and Fees</label> 
<input type="number" class="form-control" id="estimatedTaxesAndFees" placeholder="$0" onkeypress="return isNumberKey(event)" onBlur="addCommas(this)"/> 

JS:

$(document).ready(function() { 
    $(function() { 
     $("body").on("blur", "#vehiclePrice", function() { 
      updateTotalNetVehicle(); 
     }); 
     var updateTotalNetVehicle = function() { 
      var input1 = parseInt($('#vehiclePrice').val()) || 0; 
      var number1 = 0.10; 
      var sum = input1 * number1; 
      $('#estimatedTaxesAndFees').text('$' + sum.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')); 
     }; 
    }); 
}); 

Fiddle

+0

'未捕获的ReferenceError:addCommas未定义' –

+0

这是为了别的...我的坏。更新小提琴:http://jsfiddle.net/icweat/4aufj/3/ –

回答

1

固定。新JSFiddle

  1. 从html中删除onBlur ='addComas'。
  2. 将函数声明移到document.ready作用域之外。
  3. 改变$( '#estimatedTaxesAndFees')。文字$( '#estimatedTaxesAndFees')。VAL
  4. 改听事件

$( “#vehiclePrice”)。在( “模糊”, function(){ updateTotalNetVehicle(); });

$(document).ready(function() { 
     $(function() { 
      $("#vehiclePrice").on("blur", function() { 
       updateTotalNetVehicle(); 
      }); 

     }); 
    }); 

    var updateTotalNetVehicle = function() { 
       var input1 = parseInt($('#vehiclePrice').val()) || 0; 
       var number1 = 0.10; 
       var sum = input1 * number1; 
       $('#estimatedTaxesAndFees').val('$' + sum.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')); 
      }; 
+0

工作,因为我需要它谢谢你。有4分钟才能接受答案。 –

+0

@isaacweathers很高兴能帮到你!请标记正确的答案,如果它解决了你的问题:) – Yani

0

#estimatedTaxesAndFees是输入元件,从而.text()将无法​​正常工作

使用.val()代替。

$('#estimatedTaxesAndFees') 
    .val($('#estimatedTaxesAndFees').text('$' + sum.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')); 

中的jsfiddle你也有一些方法从没有在代码中存在的内联属性称为