gpt4 book ai didi

jquery parseFloat、parseInt

转载 作者:行者123 更新时间:2023-11-30 23:51:38 28 4
gpt4 key购买 nike

我有以下情况,我正在使用jquery,我需要总结表单上的一些字段。我在小计字段总计字段中发现了 NaN 错误。我已尽一切可能避免此类错误,我只需要此字段上的 SUM 。我的表单中的所有内容都工作正常,只有这两个字段有问题。我正在使用 parseFloat() 并且没有响应。仅包含 NaN

的字段

按照我的 JavaScript 代码操作:

$(document).ready( function() {

$('#valor, #taxa, #imposto, #envio, #taxa_adicional, #subtotal, #total').blur(function(){

// exemplo antigo var val = $('#valor').val();
var val = $('#valor').format({format:"#,###.00", locale:"br"});
var tax = $('#taxa').format({format:"#,###.00", locale:"br"});
var imp = $('#imposto').format({format:"#,###.00", locale:"br"});
var env = $('#envio').format({format:"#,###.00", locale:"br"});
var xat = $('#taxa_adicional').format({format:"#,###.00", locale:"br"});

if(val == "") val = 0;
if(tax == "") tax = 0;
if(imp == "") imp = 0;
if(env == "") env = 0;
if(xat == "") xat = 0;

var subtotal = parseFloat("val") + parseFloat("tax") + parseFloat("imp") + parseFloat("env");
var total = parseFloat(val) + parseFloat(tax) + parseFloat(imp) + parseFloat(env) + parseFloat(xat);

$('#subtotal').format({format:"#,###.00", locale:"br"});
$('#total').val(total);
})

});

预先感谢您就此事提供的任何帮助! :-/

警告:我正在使用名为:
的插件jquery.numberformatter - Michael Abernethy 编写的 jQuery 中的数字格式化/解析

最佳答案

HTML:

<div id="box">    
<p> Valor: <input id="valor"> </p>
<p> Taxa: <input id="taxa"> </p>
<p> Imposto: <input id="imposto"> </p>
<p> Envio: <input id="envio"> </p>
<p> Taxa adicional: <input id="taxa_adicional"> </p>
<p> Subtotal: <span id="subtotal">0</span> </p>
<p> Total: <span id="total">0</span> </p>
</div>

JavaScript:

var options = {
format: '#,###.00',
locale: 'br'
},
inputs = $( 'input:text', '#box' ).get(),
input_adicional = $( '#taxa_adicional' )[0],
input_total = $( '#total' )[0],
input_subtotal = $( '#subtotal' )[0];

然后:

$( inputs ).add( [ input_total, input_subtotal ] ).format( options );

$( inputs ).
blur( function () {
var total = 0,
subtotal = 0;

// on blur, format the field
$( this ).format( options );

// calculate the sum of all input fields
$( inputs ).each( function () {
total += +$( this ).parse( options );
});

// subtotal doesn't include the "additional" field
subtotal = total - $( input_adicional ).parse( options );

// populate the SPAN's with the sums and format the nubmers
$( input_subtotal ).text( subtotal ).format( options );
$( input_total ).text( total ).format( options );
}).
focus( function () {
// if the field contains the value 0, empty it
if ( +$( this ).parse( options ) === 0 ) {
this.value = '';
}
});

现场演示: http://jsfiddle.net/U9V6x/

关于jquery parseFloat、parseInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269775/

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