我有以下情况,我使用jquery,我需要总结一下我的表单上的一些字段。我在我的NaN小计字段和总字段中发现了错误。为了避免这种错误,已经尝试了一切可能的方法,我只需要这个字段的和。我表格中的一切都很好,只有这两个字段有问题。我使用的是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 - jQuery中的格式/解析数字,由迈克尔·阿伯内西编写
发布于 2010-11-24 19:00:31
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/
发布于 2010-11-24 18:14:54
在计算小计时,您是在一系列字符串上调用parseFloat(),而不是变量本身。这一行应是:
var subtotal = parseFloat(val) + parseFloat(tax) + parseFloat(imp) + parseFloat(env);发布于 2010-11-24 18:19:13
看起来你在使用这个插件:http://code.google.com/p/jquery-numberformatter/
该插件将接受一个元素,根据格式解析值,并重新插入DOM,据我所知。所以我假设情况就是这样。
$(document).ready( function() {
$('#valor, #taxa, #imposto, #envio, #taxa_adicional, #subtotal, #total').blur(function(){
/*
// first update all fields to have the correct format, so we can work with them later.
$('#valor').format({format:"#,###.00", locale:"br"});
$('#taxa').format({format:"#,###.00", locale:"br"});
$('#imposto').format({format:"#,###.00", locale:"br"});
$('#envio').format({format:"#,###.00", locale:"br"});
$('#taxa_adicional').format({format:"#,###.00", locale:"br"});
*/
//separating because I'm pretty sure this would work in place of all those lines of code:
// needs testing though. If this doesn't work uncomment the previous block and delete this line
$('#valor,#taxa,#imposto,#envio,#taxa_adicional').format({format:"#,###.00", locale:"br"});
// then since the values have been formatted, let's extract just the floats like you wanted
// going through the document.getElementById since the jQuery wrapper is useless overhead and you don't need a jQuery object back out.
var val = parseFloat( document.getElementById('val').innerHTML );
var tax = parseFloat( document.getElementById('taxa').innerHTML );
var imp = parseFloat( document.getElementById('imposto').innerHTML );
var env = parseFloat( document.getElementById('envio').innerHTML );
var xat = parseFloat( document.getElementById('taxa_adicional').innerHTML );
if( isNaN( val ) ) val = 0;
if( isNaN( tax ) ) tax = 0;
if( isNaN( imp ) ) imp = 0;
if( isNaN( env ) ) env = 0;
if( isNaN( xat ) ) xat = 0;
var subtotal = val + tax + imp + env;
var total = val + tax + imp + env + xat;
$('#subtotal').val(subtotal).format({format:"#,###.00", locale:"br"});
$('#total').val(total).format({format:"#,###.00", locale:"br"});
});
});注意,我只是想帮助您更容易地阅读代码。可能有很多方法来编写这个来获得leaner1
减少脂肪;减少浪费;~~也许这能翻译成成语吗?
https://stackoverflow.com/questions/4269775
复制相似问题