变量保持旧的价值 [英] Variable keeps old value

查看:85
本文介绍了变量保持旧的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我第二次点击'#print'按钮时,第一次点击的值就会在实际值之前打印出来。

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

例如:
我第一次点击按钮。警告框打印出正确的值('test1')。

For example: I click button for the first time. The alert box prints out the correct value ('test1').

我第二次点击按钮。警报框打印出第一个值('test1'),然后按OK,然后在此之后,警告框打印出第二个值('test2')。

I click button for the second time. The alert box prints out first value ('test1'), then i press OK, and right after that, the alert box prints out the second value ('test2').

任何想法我做错了什么?

Any ideas what i am doing wrong?

$(".ipdate").focus(function() {

    /*$('.dateBox').hide();*/

    var tit = $(this).attr('id');
    /*var full = '#'+tit+'B';*/

    $('#dateBox').show();

    $('#print').on('click', function(){

       var bottle = $('.sday').val()+' '+$('.smon').val()+' '+$('.syear').val();
       $('#'+tit).val(bottle);
       alert(tit);

    });

    $('#close').on('click',function() {

         $('#dateBox').hide(); 

    });    
});


推荐答案

您正在注册多个事件,这就是您看到的原因多个动作!你看到了吗? :)我有类似的情况,我花了一段时间来挑选出来。您可以在发出新事件之前取消注册旧事件。

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(); 

        });



    });

这应该可以解决您的问题。使用 off 将取消注册旧事件。

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

这篇关于变量保持旧的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆