有一个功能,我想在div中显示总数,但它不起作用。有什么想法吗?
基本上,在模糊时,获取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,'));
};
});
});
答案 0 :(得分:1)
固定。新JSFiddle
$(“#vehiclePrice”)。on(“blur”,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,'));
};
答案 1 :(得分:0)
#estimatedTaxesAndFees
是一个输入元素,因此.text()
无效
改为使用.val()
。
$('#estimatedTaxesAndFees')
.val($('#estimatedTaxesAndFees').text('$' + sum.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
在jsfiddle中你也有一些从内联属性调用的方法,这些方法在代码中不存在。