js:
$(".test").focusout(function(){
var qtdCont = parseInt($(this).val());
if(qtdCont > 0)
{
var qtdProd = $(".value").val();
开发者_StackOverflow中文版 var qtdProdInt = parseInt(qtdProd);
var qtdProdTot = qtdProd-qtdCont;
$(".value").val(qtdProdTot);
}
});
Demo: Jsfiddle
i need the subtract only the fist time when he lose the focus.
because if u go's back and focusout again, the subtraction (obviously) will happen again.
how can i control that ?
thank you.
Use the data
in the dom element:
$(".test").focusout(function(){
var qtdCont = parseInt($(this).val());
if(qtdCont > 0 && $(this).data('done') == undefined)
{
var qtdProd = $(".value").val();
var qtdProdInt = parseInt(qtdProd);
var qtdProdTot = qtdProd-qtdCont;
$(".value").val(qtdProdTot);
$(this).data('done', true);
}
});
Fiddle: http://jsfiddle.net/dNEmD/19/
UPDATE
$(".test").focusout(function(){
var qtdCont = parseInt($(this).val());
if(qtdCont > 0 &&
($(this).data('done') == undefined || $(this).data('done') == false))
{
var qtdProd = $(".value").val();
var qtdProdInt = parseInt(qtdProd);
var qtdProdTot = qtdProd-qtdCont;
$(".value").val(qtdProdTot);
$(this).data('done', true);
}
});
$(".test").change(function(){ //if value was changed
$(this).data('done', false);
});
Fiddle: http://jsfiddle.net/dNEmD/27/
keep a semaphore variable at the top somewhere. It's start out false, then the first time you focus out set it true. It remains true for the rest of the programs lifespan and you do not allow certain parts of focusout code to execute when the semaphore variable is true.
精彩评论