Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

medonho

Atualizar valor

Recommended Posts

olá pessoal... seguinte

 

não ataco nada em ajax... e talvez nem seja o caso porém tbm não mando em js (bah assim complica.... :unsure: )

 

trabalho um pouco com PHP, porém estou necessitando fazer o seguinte:

 

tenho uma variável chamada crédito com valor=50.00 e que é mostrada estática ao user como R$ 50,00 logo abaixo de um formulário.

 

nesse form tem um campo que ao digitar, mascara automaticamente o valor digitado para a nossa moeda corrente.

 

tem como fazer um esquema em ajax (ou js mesmo) no sentido de: ao passo que for sendo digitado um valor no campo do form, o valor mostrado ao user (no caso R$ 50,00) no rodapé fosse sendo diminuído?

 

 

abaixo segue o código e o form

 

form

<form name='form'>	<table width=500 border=0 align=center cellpadding=0 cellspacing=1 bgcolor='#000000'>		<tr bgcolor='#FFFFFF' height=35>			<td height='30' align='right' style='padding: 5px;'><font size='2' face='Verdana, Arial, Helvetica, sans-serif'>Valor</font></td>			<td style='padding: 5px;'>			<font size='2' face='Verdana, Arial, Helvetica, sans-serif'>R$</font> <input type='text' onkeydown='FormataValor(\"valor\", 17, event)' name='valor' maxLength='22' style='width: 150px; TEXT-ALIGN: right;'>			</td>		</tr>			</table></form>

js

function FormataValor(campo,tammax,teclapres) {	var tecla = teclapres.keyCode;	vr = document.form[campo].value;	vr = vr.replace( "/", "" );	vr = vr.replace( "/", "" );	vr = vr.replace( ",", "" );	vr = vr.replace( ".", "" );	vr = vr.replace( ".", "" );	vr = vr.replace( ".", "" );	vr = vr.replace( ".", "" );	tam = vr.length;	if (tam < tammax && tecla != 8){ tam = vr.length + 1; }	if (tecla == 8 ){	tam = tam - 1; }			if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){		if ( tam <= 2 ){ 	 		document.form[campo].value = vr; }	 	if ( (tam > 2) && (tam <= 5) ){	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }	 	if ( (tam >= 6) && (tam <= 8) ){	 		document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }	 	if ( (tam >= 9) && (tam <= 11) ){	 		document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }	 	if ( (tam >= 12) && (tam <= 14) ){	 		document.form[campo].value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }	 	if ( (tam >= 15) && (tam <= 17) ){	 		document.form[campo].value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}	}		}

em tempo... estou disposto tbm a aprender mais sobre ajax e js. aguma dica para leitura e aprendizagem? http://forum.imasters.com.br/public/style_emoticons/default/thumbsup.gif

Compartilhar este post


Link para o post
Compartilhar em outros sites

Amigo, para fazer isso não precisa de Ajax, vou move-lo para JS, mas ai vai uma idéia de como funcionar

 

JS

 

<script>function FormataValor(campo,tammax,teclapres) {	var tecla = teclapres.keyCode;	vr = document.form[campo].value;	vr = vr.replace( "/", "" );	vr = vr.replace( "/", "" );	vr = vr.replace( ",", "" );	vr = vr.replace( ".", "" );	vr = vr.replace( ".", "" );	vr = vr.replace( ".", "" );	vr = vr.replace( ".", "" );	tam = vr.length;	if (tam < tammax && tecla != 8){ tam = vr.length + 1; }	if (tecla == 8 ){	tam = tam - 1; }			if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){		if ( tam <= 2 ){			 document.form[campo].value = vr; }		 if ( (tam > 2) && (tam <= 5) ){			 document.form[campo].value = vr.substr( 0, tam - 2 ) + '.' + vr.substr( tam - 2, tam ); }		 if ( (tam >= 6) && (tam <= 8) ){			 document.form[campo].value = vr.substr( 0, tam - 5 ) + ',' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ); }		 if ( (tam >= 9) && (tam <= 11) ){			 document.form[campo].value = vr.substr( 0, tam - 8 ) + ',' + vr.substr( tam - 8, 3 ) + ',' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ); }		 if ( (tam >= 12) && (tam <= 14) ){			 document.form[campo].value = vr.substr( 0, tam - 11 ) + ',' + vr.substr( tam - 11, 3 ) + ',' + vr.substr( tam - 8, 3 ) + ',' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ); }		 if ( (tam >= 15) && (tam <= 17) ){			 document.form[campo].value = vr.substr( 0, tam - 14 ) + ',' + vr.substr( tam - 14, 3 ) + ',' + vr.substr( tam - 11, 3 ) + ',' + vr.substr( tam - 8, 3 ) + ',' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam );}	}}function calcula(valor) {	if(valor) {		alert(parseFloat(valor));		document.getElementById('valortotal').value = parseFloat(document.getElementById('valorfixo').value) - parseFloat(valor);	}}</script>

HTML

 

<form name='form'>  Valor fixo   <input name="valorfixo" type="text" disabled id="valorfixo" value="1000.00">  <table width=500 border=0 align=center cellpadding=0 cellspacing=1 bgcolor='#000000'>		<tr bgcolor='#FFFFFF' height=35>			<td height='30' align='right' style='padding: 5px;'><font size='2' face='Verdana, Arial, Helvetica, sans-serif'>Valor</font></td>			<td style='padding: 5px;'>			<font size='2' face='Verdana, Arial, Helvetica, sans-serif'>R$</font> <input type='text' onKeyUp="calcula(this.value);" onkeydown='FormataValor("valor", 17, event)' id='valor' name='valor' maxLength='22' style='width: 150px; TEXT-ALIGN: right;'>			</td>		</tr>			</table>  <p> Valor total 	<input name="valortotal" type="text" id="valortotal" disabled>  </p></form>

Testa ai e vê se funciona

 

Valeu!!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Sim, tem como é só substituir a linha

 

<input name="valortotal" type="text" id="valortotal" disabled>

pela linha

 

<div id="valortotal"></div>

e a linha

 

document.getElementById('valortotal').value = parseFloat(document.getElementById('valorfixo').value) - parseFloat(valor);

pela linha

 

document.getElementById('valortotal').innerHTML = parseFloat(document.getElementById('valorfixo').value) - parseFloat(valor);

Valeu!!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.