开发者

how do not duplicate a action in jquery

开发者 https://www.devze.com 2023-03-11 12:08 出处:网络
js: $(\".test\").focusout(function(){ var qtdCont = parseInt($(this).val()); if(qtdCont > 0) { var qtdProd= $(\".value\").val();

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消