我是MVC和javascript新手,并且遇到以下问题。
我有带有货币下拉菜单的发票应用程序,我需要计算发票金额,这里我有两个关于金额计算的任务:
当我选择外币(EUR,USD)时,我想计算不含增值税和增值税金额的总和,也需要清除增值税和增值税金额的输入值(我需要将其为空或类似的内容“ /”。
当我选择MKD货币时,我要计算包括的增值税和增值税金额。
话虽如此,任务2正在运行,但是我在任务1上遇到了麻烦-控制台中未显示外币计算的值,只有在我选择MKD货币并选择VAT后,该值才显示汇率),然后将VAT更改为0,然后再次将货币更改为EUR或USD,只有这样,VatAmount值才会在索引中清除(而VAT仍然有值)。
我希望有人可以帮助我解决这个问题(我已经努力了2-3天)。如果您在理解我的解释时遇到困难,请进行澄清。
谢谢!
剃刀视图:
<div class="col-md-6">
<div class="col-md-6 col-md-offset-4">
@Html.LabelFor(model => model.Invoice.Subtotal, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.Invoice.Subtotal, new { htmlAttributes = new { @class = "form-control", @id = "Subtotal", @placeholder = "Subtotal", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Invoice.Subtotal, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6 col-md-offset-4">
@Html.LabelFor(model => model.Invoice.Currency, htmlAttributes: new { @class = "control-label" })
<div>
@Html.DropDownListFor(model => model.Invoice.Currency, ViewData["currency"] as List<SelectListItem>, new { @class = "form-control", @id = "currency" })
@Html.ValidationMessageFor(model => model.Invoice.Currency, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6 col-md-offset-4 vat" >
@Html.LabelFor(model => model.Invoice.VAT, htmlAttributes: new { @class = "control-label" })
<div>
@Html.DropDownListFor(model => model.Invoice.VAT, ViewData["vat"] as List<SelectListItem>, new { @class = "form-control", @id = "vat" })
@Html.ValidationMessageFor(model => model.Invoice.VAT, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6 col-md-offset-4 VatAmount" >
@Html.LabelFor(model => model.Invoice.VatAmount, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.Invoice.VatAmount, new { htmlAttributes = new { @class = "form-control", @id = "VatAmount", @placeholder = "VAT amount", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Invoice.VatAmount, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6 col-md-offset-4">
@Html.LabelFor(model => model.Invoice.TotalAmount, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.Invoice.TotalAmount, new { htmlAttributes = new { @class = "form-control", @placeholder = "Total Amount", @id = "TotalAmount", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Invoice.TotalAmount, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6 col-md-offset-4">
@Html.LabelFor(model => model.Invoice.AmountPaid, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.Invoice.AmountPaid, new { htmlAttributes = new { @class = "form-control", @placeholder = "Amount Paid", @id = "AmountPaid" } })
@Html.ValidationMessageFor(model => model.Invoice.AmountPaid, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6 col-md-offset-4">
@Html.LabelFor(model => model.Invoice.BalanceDue, htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.Invoice.BalanceDue, new { htmlAttributes = new { @class = "form-control", @id = "BalanceDue", @placeholder = "Balance Due", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Invoice.BalanceDue, "", new { @class = "text-danger" })
</div>
脚本
//total calculation
function calculateSum() {
var sum = 0;
// iterate through each td based on class and add the values
$(".price").each(function () {
var value = $(this).text();
// add only if the value is number
if (!isNaN(value) && value.length != 0) {
sum += parseFloat(value);
}
});
$('#Subtotal').val(sum);
var a = $('#TotalAmount').val();
var b = $('#AmountPaid').val();
$('#BalanceDue').val(a - b);
$('#VatAmount').val(sum);
var a = $('#vat').val();
var b = $('#Subtotal').val();
$('#BalanceDue').val(a * b);
$('#TotalAmount').val(sum);
var a = $('#VatAmount').val();
var b = $('#AmountPaid').val();
$('#BalanceDue').val(a + b);
};
$("#vat").on("change", function () {
var selectedValue = $(this).val();
calculateTax(+selectedValue)
});
function calculateTax(vatPercent) {
var subTotal = +$('#Subtotal').val();
var vatAmount = subTotal * (vatPercent / 100);
console.log('vat amount:', vatAmount);
$('#VatAmount').val(vatAmount);
var totAmount = +$('#TotalAmount').val();
var totAmount = subTotal + vatAmount;
console.log('total amount:', totAmount);
$('#TotalAmount').val(totAmount);
$('#BalanceDue').val(totAmount);
if (isForeignCurrency()) {
alert("nope");
var subTotal = $("#SubTotal").val();
var amountPaid = $("#AmountPaid").val();
$("#VatAmount").val("");
$("#vat").val("");
$("#BalanceDue").val(subTotal - amountPaid);
} else {
calculateSum();
}
}
$('.price').each(function () {
calculateSum();
});
$('#AmountPaid').keyup(function () {
var a = $('#Subtotal').val();
var b = $('#AmountPaid').val();
$('#BalanceDue').val(a - b);
});
//on currency change hide vat and vatAmount
$('#currency').change(function () {
if (isForeignCurrency()) {
$('.vat').hide();
$('.VatAmount').hide();
} else {
$('.vat').show();
$('.VatAmount').show();
}
});
现在,我需要脚本最后部分的帮助:
function isForeignCurrency() {
var value = $('#currency').val();
if (value == 'MKD') {
return false;
} else {
return true;
}
};
if (isForeignCurrency()) {
var subTotal = $("#SubTotal").val();
$("#VatAmount").val("");
$("#vat").val("");
$("#BalanceDue").val(subTotal);
} else {
calculateSum();
}