to修复了返回错误输出的问题



function get_tax(rent) {
var pcharges = parseFloat($('#pcharges').val());
pcharges = pcharges.toFixed(2);
rent = parseFloat(rent);
rent = rent.toFixed(2);
var tax = parseFloat((rent * 15) / 100);
tax = tax.toFixed(2);
$('#tax').val(tax);
var tot_rent = pcharges + rent + tax;
// alert(tot_rent);
$('#tot_rent').val(tot_rent);
// alert(tax);
}
function get_total(pcharges) {
pcharges = parseFloat(pcharges);
old_tot = parseFloat($('#tot_rent').val());
// alert(pcharges+old_tot);
$('#tot_rent').val(pcharges + old_tot);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rent:<input required onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" onchange="get_tax(this.value)" type="text" name="rent">
Tax:<input required onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" id="tax" type="text" name="tax">
Phone Charges:<input value="0" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" required id="pcharges"  onchange="get_total(this.value)" type="text" name="pcharges">
Total Rent:<input required onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" id="tot_rent" type="text" name="tot_rent">

我想从用户那里获得租金,并在税中显示15%,用户将手动支付电话费,然后我需要添加租金、税和电话费。税收到位,但总租金没有到位

您正在尝试添加字符串值(toFixed()返回字符串(,该值将连接这些值,使用一元加(+(转换为浮点值,然后执行加法。

function get_tax(rent) {
var pcharges = parseFloat($('#pcharges').val());
pcharges = +pcharges.toFixed(2);
rent = parseFloat(rent);
rent = +rent.toFixed(2);
var tax = parseFloat((rent * 15) / 100);
tax = +tax.toFixed(2);
$('#tax').val(tax);
var tot_rent = pcharges + rent + tax;
// alert(tot_rent);
$('#tot_rent').val(tot_rent);
// alert(tax);
}
function get_total(pcharges) {
pcharges = parseFloat(pcharges);
old_tot = parseFloat($('#tot_rent').val());
// alert(pcharges+old_tot);
$('#tot_rent').val(pcharges + old_tot);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rent:<input required onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" onchange="get_tax(this.value)" type="text" name="rent">
Tax:<input required onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" id="tax" type="text" name="tax">
Phone Charges:<input value="0" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" required id="pcharges"  onchange="get_total(this.value)" type="text" name="pcharges">
Total Rent:<input required onkeyup="this.value=this.value.replace(/[^0-9.]/g,'');" id="tot_rent" type="text" name="tot_rent">

这是因为.toFixed()返回string值。因此,如果您添加多个string值,它将连接

更改

var tot_rent = pcharges + rent + tax;

var tot_rent = var tot_rent = parseFloat( pcharges) + parseFloat(rent) + parseFloat(tax);

演示在这里

最新更新