模糊触发变更事件

模糊触发变更事件

我有以下代码:

$('.subtle-input').live('blur', function() {
    // this will fire all the time
});

$('.provider-rate-card input[type="text"]').live('change', function() {
    // this will fire some of the time, only when value is changed.
});


每当出现blur函数时,如何确保change函数发生在change函数之前?

最佳答案

查看以下jsfiddle

<div class="provider-rate-card">
    <input class="subtle-input" type="text" />
</div>




$(function() {


    //This closure is simply here because you will be using a global variable
    //It helps prevent pollution of the global namespace, which is a good
    //practice in Javascript.
    (function() {
        //defines the flag
        var changed = false;

        $('.subtle-input').on('blur', function() {
            blurFunction(changeFunction);
        });

        $('.provider-rate-card input[type="text"]').on('change', function() {
            //sets the flag
            changed = true;
        });

        function blurFunction(func) {
            //do something every time it blurs
            console.log('blurred');

            //you will provide this callback as a parameter (see above)
            return func();
        }

        function changeFunction() {
            //checks the flag
            if(changed === true) {
                //do something whenever the value changes
                console.log('changed');

                //reset
                changed = false;
            }
        }
    })();

});

关于javascript - 模糊触发变更事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20846461/

10-12 06:39