Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa tarde
estou fazendo soma de campos R$ com JS
gostaria de saber como posso fazer para que quando a variavel estiver vasia receba o valor = 0
if(valorA == "") valorA = 0;
if(valorB == "") valorB = 0;
if(valorC == "") valorC = 0;
if(valorD == "") valorD = 0;
if(valorE == "") valorE = 0;
<script type="text/javascript">
function id( el ){
return document.getElementById( el );
}
function getMoney( el ){
var money = id( el ).value.replace( ',', '.' );
return parseFloat( money )*100;
}
function soma()
{
var total = getMoney('valorA')-getMoney('valorB')+getMoney('valorC')+getMoney('valorD');
id('ValorTotal').value = ''+total/100;
}
</script>Resolvido
function soma () {
valorA = document.getElementById('valorA').value;
valorB = document.getElementById('valorB').value;
valorC = document.getElementById('valorC').value;
valorD = document.getElementById('valorD').value;
valorA = valorA.toString ().replace( ".", "" );
valorA = valorA.toString ().replace( ",", "." );
valorB = valorB.toString ().replace( ".", "" );
valorB = valorB.toString ().replace( ",", "." );
valorC = valorC.toString ().replace( ".", "" );
valorC = valorC.toString ().replace( ",", "." );
valorD = valorD.toString ().replace( ".", "" );
valorD = valorD.toString ().replace( ",", "." );
if(valorA == "") valorA = 0;
if(valorB == "") valorB = 0;
if(valorC == "") valorC = 0;
if(valorD == "") valorD = 0;
ValorTotal = parseFloat (valorA) - parseFloat (valorB) +parseFloat (valorC) + parseFloat (valorD) ;
ValorTotal = ValorTotal.toFixed(2);
ValorTotal = ValorTotal.toString().replace( ".", "," );
document.getElementById('ValorTotal').value = ValorTotal;
};
voce pode tentar o length