jquery插件中的fire功能?

时间:2015-03-20 04:52:59

标签: javascript jquery events plugins bind

我为某些< input>动态创建了递增和递减按钮。 html元素(特别是数字输入),但我不能为我的生活如何弄清楚如何通过插件函数绑定一个动作。

我想以这种方式专门做,因为这些箭头将用于许多不同类型的形式,这些形式减去并添加影响某些视觉元素的值(例如%,金钱等)。

该功能围绕以下几行:

(function ($) {
    $.fn.addIncrementArrows = function (min, max, interval, fastInterval, event) {

        var input = this;

        /*Other building things go here*/

        var timeoutDownArrow;
        var intervalDownArrow;
        //clicking button down for 1 second fires '-' actions
        $(this).parent().children(".downarrow").mousedown(function () {
            timeoutDownArrow = setTimeout(function () {
                intervalDownArrow = setInterval(function () {
                    changeValue(-fastInterval);

                    //how to do event here?

                }, 90);
            }, 750);
        }).bind("mouseup mouseleave", function () {
            clearTimeout(timeoutDownArrow);
            clearInterval(intervalDownArrow);
        });

    };
}(jQuery));

我需要声明要使其具有如下箭头:

formnamehere.addIncrementArrows(0, 100, 1, 5, event);

如果可能:我如何将动作绑定到.addIncrementArrows()中的按钮?

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我得出了一个结论:

1 - 将函数更改为.addIncrementArrows()作为命名运行时函数运行:

var setSliderPercentage = function (percent) {
    //do stuff here
}

2 - 将函数作为变量名称传递给插件

percentageForm.addIncrementArrows(0, 100, 1, 5, setSliderPercentage);

3 - 在addIncrementArrows()插件中传递传递的函数。

$.fn.addIncrementArrows = function (min, max, interval, fastInterval, event) {
    var input = this;
    if (max === false) max = Infinity; //infinity and beyond!

    //Other stuff here

    //make the it auto increment or decrement every 100ms when 'holding down' the button after 1 second
    var timeoutUpArrow;
    var intervalUpArrow;
    $(uparrow).mousedown(function () {
        timeoutUpArrow = setTimeout(function () {
            intervalUpArrow = setInterval(function () {
                changeValue(fastInterval);
                //
                if (typeof event != "undefined") event(input.val());
                //
            }, 90);
        }, 750);
    }).bind("mouseup mouseleave", function () {
        clearTimeout(timeoutUpArrow);
        clearInterval(intervalUpArrow);
    });

}
相关问题