gpt4 book ai didi

javascript - Jquery 添加计算值

转载 作者:行者123 更新时间:2023-12-02 15:01:04 24 4
gpt4 key购买 nike

我有一个表单,用户输入重量,其余字段自动计算。让一切正常工作,除了总计 - 它采用最后 2 个计算字段并将它们加在一起。表单加载后,我会在字段中得到 NaN。如有任何帮助,我们将不胜感激。

jQuery(function () {
var min = 'Min';
var total1 = .20;
var total2 = .17;
var total3 = .15;
var total4 = .13;
var total5 = .09;

$('#weight').change(function () {
var w = +$('#weight').val();
var total;
if (w < 425) {
price = min;
} else if (w < 1000) {
price = total1;
}
else if (w < 2200) {
price = total2;
}
else if (w < 4400) {
price = total3;
}
else if (w < 11000) {
price = total4;
}
else if (w > 11000) {
price = total5;
}
$('#rate').val(price);
})
})

jQuery(function () {
var min = 85.00;
var total1 = .20;
var total2 = .17;
var total3 = .15;
var total4 = .13;
var total5 = .09;

$('#weight').change(function () {
var w = +$('#weight').val();
var total;
if (w < 425) {
subtotal = min;
} else if (w < 1000) {
subtotal = w * total1;
}
else if (w < 2200) {
subtotal = w * total2;
}
else if (w < 4400) {
subtotal = w * total3;
}
else if (w < 11000) {
subtotal = w * total4;
}
else if (w > 11000) {
subtotal = w * total5;
}
$('#subtotal').val(subtotal.toFixed(2));
})
})

jQuery(function () {
var iva = .16;

$('#weight').change(function () {
var s = +$('#subtotal').val();

var total;
if (s <= 85) {
ivacalc = 85 * iva;
} else if (s > 85) {
ivacalc = s * iva;
}
$('#iva').val(ivacalc.toFixed(2));
})
})

jQuery(function () {

var a = parseFloat($('#subtotal').val());
var b = parseFloat($('#iva').val());
$('#total').val(a + b);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label for="weight">Total Weight (in lbs)</label>
<input id="weight">
</div>
<div>
<label for="rate">Rate Per Lb</label>
<input id="rate">
</div>
<div>
<label for="subtotal">Sub Total </label>
<input id="subtotal">
</div>

<div>
<label for="iva">IVA </label>
<input id="iva">
</div>

<div>
<label for="total">Total </label>
<input id="total">
</div>

最佳答案

试试这个(假设您在字段中输入正确并且该功能被触发):

//Initialize variables and get values from the input field
var subtotal = $('#subtotal').val();
var iva = $('#iva').val();

//Assign to another variable
var a = parseFloat(subtotal);
var b = parseFloat(iva);
var total = a+b;
$('#total').val(total);

关于javascript - Jquery 添加计算值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com