简体   繁体   English

在jQuery插件的事件函数中获取变量

[英]Get a variable inside an event function of a jquery plugin

I've developed a plugin using jQuery for my project. 我已经为我的项目使用jQuery开发了一个插件。 In this plugin I use a slider plugin which initialize when my plugin is initialized. 在此插件中,我使用了一个滑动插件,该插件在初始化我的插件时会初始化。 I assign it to a public variable in the plugin. 我将其分配给插件中的公共变量。 And there is an event bound to an element which get's fired when the user enters some amount. 并且有一个绑定到元素的事件,当用户输入一定数量时会触发该事件。 basically, the slider changes according to the value entered in the input element. 基本上,滑块根据在输入元素中输入的值而变化。 But the problem is, the variable which I assigned the slider is undefined when the keyup event get fired. 但是问题是,当触发keyup事件时,我为滑块分配的变量未定义。 How can I fix this. 我怎样才能解决这个问题。

inti method 方法

var init = function() {
        plugin.settings = $.extend({}, defaults, options);

        set_default_values();

        this.slider = $(plugin.settings.sliderElements).slider();

        $(plugin.settings.sliderElements).on("slide", plugin.settings.onPluginSlide);
        $(plugin.settings.numOfMonthsKey).on("keyup", function(e){
            plugin.settings.onMonthKeyEnter(e,this.slider);
        });
}

the events 事件

var defaults = {
        sliderElements: '#numOfMonths', //comma seperated jquery selectors
        perMonthAmountElement: '.perMonth',
        numOfMonthsElements: '.numOfMon', //comma seperated jquery selectors
        sumOfRecItemElement: '.sumOfRecItems',
        monthlyCostElement: '.monthlyCost',
        oneTimeFeeElement: '.oneTimeFee',
        nextInvoiceAmountElement: '.nextInvoiceAmount',
        numOfMonthsKey: '.numOfMonthsKey',
        oneTimeFee : 0,
        sumOfRecurringItems : 0,
        interestRate: 0.2,
        currency : 'kr', //This is just to display 
        onPluginSlide: function(sliderEvent){
            $(plugin.settings.numOfMonthsElements).text(sliderEvent.value);
            $(plugin.settings.perMonthAmountElement).text(calc_compound_interest_rate(plugin.settings.oneTimeFee,plugin.settings.interestRate,sliderEvent.value));
            $(plugin.settings.monthlyCostElement).text(plugin.settings.sumOfRecurringItems + amount_per_month(sliderEvent.value));
        },
        onMonthKeyEnter: function(keyEvent,slider){
            slider.slider('setValue', keyEvent.getCurrentTarget.value ,true)
        }
    }

How can I access the slider object after plugin initialization. 插件初始化后如何访问滑块对象。 I thought of using a window variable bt I have no idea whether it's good or bad. 我想到了使用窗口变量bt,不知道它是好是坏。

Any help would be appreciated. 任何帮助,将不胜感激。

The context of 'this' keyword changes inside the event. “ this”关键字的上下文在事件内部发生变化。 Using 'this' keyword can be a bit confusing in JavaScript, I would recommend reading the following answer - How does the "this" keyword work? 在JavaScript中使用'this'关键字可能会造成一些混乱,我建议阅读以下答案- “ this”关键字如何工作? .

I would avoid using a window (global) variable, they can easily conflict with other JavaScript libraries you might be using/use in the future. 我会避免使用window(全局)变量,因为它们很容易与将来可能使用/使用的其他JavaScript库发生冲突。

You could try making a self variable (or whatever other name you think is appropriate) in your init function instead, and us that in place of 'this' keyword. 您可以尝试在init函数中创建一个自变量(或任何您认为合适的其他名称),并使用'this'关键字代替我们。 This will avoid the issue of 'this' keyword having a different value depending on context, your self variable will always be the same. 这样可以避免'this'关键字根据上下文具有不同的值的问题,您的自变量将始终相同。

var init = function() {
    var self = this;
    self.slider = $(plugin.settings.sliderElements).slider();

    $(plugin.settings.numOfMonthsKey).on("keyup", function(e){
        plugin.settings.onMonthKeyEnter(e,self.slider);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM