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,
tenho um form onde quero fazer o aumento do valor que esta dentro por exemplo tenho o numero 1 carrego no + e aumenta pa 2 se carregar no – ele altera para 0… ate ai tudo bem não há problema a questão é quando fica a 0 se carregar em – ele vai para -1 e não pode, não pode mostrar números negativos.
Alguém me pode ajudar?
<input type="text" name="qty" value="0" style=" width:30px;" />
<script type="text/javascript">
$(function(){
$("#inc").click(function(){
$(":text[name='qty']").val( Number($(":text[name='qty']").val()) + 1 );
});
$("#dec").click(function(){
$ (":text[name='qty']").val( Number($(":text[name='qty']").val()) - 1 );
});
});
</script>
<img src="imgs/aumentar.png" id="inc"/>
<img src="imgs/diminuir.png" id="dec"/>>
antes de mudar, verifique se o atual é maior que 0, se não for, você não faz nada.
um simples if()
sim bruno mas ja tentei fazer isso e nao consegui pq nao sei como retomar o valor
mostre como você tentou, pois é super simples, e o teu script já dá a dica de como fazer.
$("#dec").click(function(){
var novo_n = Number($(":text[name='qty']").val()) - 1;
if( novo_n > 0 )
$ (":text[name='qty']").val( novo_n );
});Obrigado Bruno,
O meu problema estava na var, não estava a fazer mesmo nada bem…
Estou tentando fazer outra coisa que é como este exemplo
O cliente escolhe um valor e calcula logo esse valor sobre o preço sem refrescar a pagina, como posso fazer isso ?
encontrei um script e estou a alterar para o k eu quero
<script type="text/javascript">
var bIsFirebugReady = (!!window.console && !!window.console.log);
$(document).ready(
function (){
$("#idPluginVersion").text($.Calculation.version);
$("input[name^=qty_item_]").bind("keyup", recalc);
recalc();
$("input[name^=sum]").sum("keyup", "#totalSum");
$("input[name^=avg]").avg({
bind:"keyup"
, selector: "#totalAvg"
, onParseError: function(){
this.css("backgroundColor", "#cc0000")
}
, onParseClear: function (){
this.css("backgroundColor", "");
}
});
$("input[name^=min]").min("keyup", "#numberMin");
$("input[name^=max]").max("keyup", {
selector: "#numberMax"
, oncalc: function (value, options){
$(options.selector).val(value);
}
});
$("#idTotalTextSum").click(
function (){
var sum = $(".textSum").sum();
$("#totalTextSum").text("$" + sum.toString());
}
);
$("#idTotalTextAvg").click(
function (){
var avg = $(".textAvg").avg();
$("#totalTextAvg").text(avg.toString());
}
);
}
);
function recalc(){
$("[id^=total_item]").calc(
"qty * price",
{
qty: $("input[name^=qty_item_]"),
price: $("[id^=price_item_]")
},
// define the formatting callback, the results of the calculation are passed to this function
function (s){
// return the number as a dollar amount
return "" + s.toFixed(0);
},
// define the finish callback, this runs after the calculation has been complete
function ($this){
// sum the total of the $("[id^=total_item]") selector
var sum = $this.sum();
$("#grandTotal").text(
// round the results to 2 digits
sum.toFixed(2)+" €"
);
}
);
}
</script>
<form action="" method="post" id="frmCreateCheckboxRange" onSubmit="return false;">
<img src="imgs/traco_comprar_pequeno.png" width="75" height="1"><p>
comprar unidade <br />
<input type="radio" name="botao2">
<input type="text" name="qty_item_1" id="qty_item_1" value="" style="width:25px; height:10px; font-size:11px;"/>
<img src="imgs/seta2.png" />
<input type="text" name="peso" id="total_item_1" value="0" style="width:40px; height:10px; font-size:11px;"><span style="font-family: Tahoma, Geneva, sans-serif; font-size:12px; padding-left:2px;">gr</span><br />
até aqui tudo bem nao há problema nenhum, so que eu quero é que ele calcule o valor que esta em id="qty_item_1" * 30 por exemplo e mostre em <div id="grandTotal"></div>.
neste momento esta a imprimir em <div id="grandTotal"></div> o valor que está em id="total_item_1"
alguem sabe como fazer
ja tenho o form correcto so que queria por uma função que é o cliente escolher a forma como quer comprar as coisa ou por peso ou por unidades. para isso tenho um type="radio" onde o cliente escolhe. ja tive a ajuda do bruno a dizer que era um que por um if mas nao consegui, experimentei por esse isso logo no inicio mas so dava se dizer o f5 a pagina, e eu queria era dinamino mal escolher o radio o sistema alterava...
<script type="text/javascript">
var bIsFirebugReady = (!!window.console && !!window.console.log);
$(document).ready(
function (){
$("#idPluginVersion").text($.Calculation.version);
$("input[name^=qty_item_]").bind("keyup", recalc);
recalc();
$("input[name^=sum]").sum("keyup", "#totalSum");
$("input[name^=avg]").avg({
bind:"keyup"
, selector: "#totalAvg"
, onParseError: function(){
this.css("backgroundColor", "#cc0000")
}
, onParseClear: function (){
this.css("backgroundColor", "");
}
});
$("input[name^=min]").min("keyup", "#numberMin");
$("input[name^=max]").max("keyup", {
selector: "#numberMax"
, oncalc: function (value, options){
$(options.selector).val(value);
}
});
$("#idTotalTextSum").click(
function (){
var sum = $(".textSum").sum();
$("#totalTextSum").text("$" + sum.toString());
}
);
$("#idTotalTextAvg").click(
function (){
var avg = $(".textAvg").avg();
$("#totalTextAvg").text(avg.toString());
}
);
}
);
function recalc(){
$("[id^=total_item]").calc(
"qty * price",
{
qty: $("input[name^=qty_item_]"),
price: $("[id^=price_item_]")
}
,
// define the formatting callback, the results of the calculation are passed to this function
function (s){
// return the number as a dollar amount
return "" + s.toFixed(0);
},
// define the finish callback, this runs after the calculation has been complete
function ($this){
var sum = $this.sum();
$("#grandTotal").text(
((sum.toFixed(2)*6.99)/1000)
);
}
);
}
</script>
<form action="" method="post" id="frmCreateCheckboxRange" onSubmit="return false;">
<img src="imgs/traco_comprar_pequeno.png" width="75" height="1"><p>
comprar unidade <br />
<input type="radio" name="botao2" id="um">
<input type="text" name="qty_item_1" id="qty_item_1" value="" style="width:25px; height:10px; font-size:11px;"/>
<img src="imgs/seta2.png" />
<input type="text" name="peso" id="total_item_1" value="0" style="width:40px; height:10px; font-size:11px;"><span style="font-family: Tahoma, Geneva, sans-serif; font-size:12px; padding-left:2px;">gr</span><br />
comprar ao peso <br />
<input type="radio" name="botao2" id="dois">
<input type="text" name="2qty_item_2" id="2qty_item_2" value="" style="width:25px; height:10px; font-size:11px;"/>
<img src="imgs/seta2.png" />
<input type="text" name="peso2" id="total_item_1" value="0" style="width:25px; height:10px; font-size:11px;">
<span style="font-family: Tahoma, Geneva, sans-serif; font-size:12px; padding-left:2px;">gr</span>
<div id="grandTotal"></div>
ja esp if( $('#um').attr('checked') ){ .... entrar toda a parte do java script com referencia a name="qty_item_1" }
else { referente a name="2qty_item_1" }
mas nao consigo por a dar como quero , alguém me pode ajudar,
obrigado a todos
antes de mudar, verifique se o atual é maior que 0, se não for, você não faz nada.
um simples if()