简体   繁体   English

在文本框中显示增值税值

[英]Display vat value in text box

Does anyone know why I'm getting a value NaN in the vat input box?有谁知道为什么我在增值税输入框中得到一个值 NaN ? When I enter a value of qty it always gives me a NaN value.当我输入一个 qty 值时,它总是给我一个 NaN 值。

$('#sales_qty').keyup(function(){
    var qty = parseFloat($('#sales_qty').val()) || 0;
    var sub_total = parseFloat($('#sales_sub_total').val()) || 0;
    var vat = 0.12;

    var sales_total = $('#sales_total').val((qty * sub_total).toFixed(2));

    $('#sales_vat').val((sales_total * vat).toFixed(2));
});

JSFiddle: https://jsfiddle.net/yv6zks1g/ JSFiddle: https://jsfiddle.net/yv6zks1g/

Because sales_total is the element itself, (not the value).因为sales_total是元素本身,(不是值)。 you should add another val() at the end to get the value.您应该在末尾添加另一个val()以获取该值。

 $('#sales_qty').keyup(function(){ var qty = parseFloat($('#sales_qty').val()) || 0; var sub_total = parseFloat($('#sales_sub_total').val()) || 0; var vat = 0.12; var sales_total = $('#sales_total').val((qty * sub_total).toFixed(2)).val(); $('#sales_vat').val((sales_total * vat).toFixed(2)); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="sales_qty" type="text" placeholder="sales_qty" /> <input id="sales_sub_total" type="text" placeholder="sales_sub_total" /> <input id="sales_total" type="text" placeholder="sales_total" /> <input id="sales_vat" type="text" placeholder="sales_vat"/>

i think its because of the var sales_total = $('#sales_total').val((qty * sub_total).toFixed(2));我认为这是因为var sales_total = $('#sales_total').val((qty * sub_total).toFixed(2)); line.线。
instead of $('#sales_total').val((qty * sub_total).toFixed(2));而不是$('#sales_total').val((qty * sub_total).toFixed(2));
add var sales_total = (qty * sub_total).toFixed(2);添加var sales_total = (qty * sub_total).toFixed(2);

As in these two lines you are parsing float value from your input elements,在这两行中,您正在从输入元素中解析浮点值,

var qty = parseFloat($('#sales_qty').val()) || 0;
var sub_total = parseFloat($('#sales_sub_total').val()) || 0;

You need to set your inputs like these,你需要像这样设置你的输入,

<input id="sales_qty" type="number" /> 
<input id="sales_sub_total" type="number" />

So that it is not possible to enter anything other than number.这样就不能输入除数字以外的任何内容。 This will get you rid of the NaN problem.这将使您摆脱 NaN 问题。

Also you need to put你也需要把

var sales_total = $('#sales_total').val((qty * sub_total).toFixed(2)).val()

Its because you are not getting the real value of sales total input and you are just assigning a new value for it.这是因为您没有获得sales total input的实际价值,而只是为其分配了一个新值。 On this line在这条线上

var sales_total = $('#sales_total').val((qty * sub_total).toFixed(2));

To fix it you have to get again the value of sales total input by using the val() method .要修复它,您必须使用val() method再次获得sales total input的值。 Your code should look like this您的代码应如下所示

$('#sales_qty').keyup(function(){
    var qty = parseFloat($('#sales_qty').val()) || 0;
    var sub_total = parseFloat($('#sales_sub_total').val()) || 0;
    var vat = 0.12;
    
    $('#sales_total').val((qty * sub_total).toFixed(2)); // assign the value
    var sales_total = $('#sales_total').val()  // grab the value

    $('#sales_vat').val((sales_total * vat).toFixed(2));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM