Variable keeps old value

后端 未结 1 763
陌清茗
陌清茗 2021-01-25 03:02

Whenever i click the \'#print\' button for the second time, the value from the first click prints out before the real value.

For example: I click button for the first t

相关标签:
1条回答
  • 2021-01-25 03:59

    You are registering multiple events and that is why you see multiple actions! Did you see that? :) I had a similar situation and it took me a while to pick those out. You can unregister for the old event before issuing the new one.

       $(".ipdate").focus(function() {
    
            /*$('.dateBox').hide();*/
    
            var tit = $(this).attr('id');
            /*var full = '#'+tit+'B';*/
    
            $('#dateBox').show();
    
            $('#print').off('click');
            $('#print').on('click', function(){
    
                var bottle = $('.sday').val()+' '+$('.smon').val()+' '+$('.syear').val();
                $('#'+tit).val(bottle);
                alert(tit);
    
            });
    
            $('#close').off('click');
            $('#close').on('click',function() {
    
               $('#dateBox').hide(); 
    
            });
    
    
    
        });
    

    This should fix your issue. Using off will unregister the old events.

    0 讨论(0)
提交回复
热议问题