[英]jquery plugin, boolean callback parameter
I am trying to pass a boolean parameter to a callback from inside a jquery plugin but parameter is always undefined. 我试图从jQuery插件内部将布尔参数传递给回调,但参数始终未定义。
The onSwitch callback parameter should alternate between true and false each time the link is clicked. 每次单击链接时,onSwitch回调参数应在true和false之间交替。 Using a debugger I can see that the value passed to callback call function is properly defined as true or false but inside the callback implementation it turns to undefined. 使用调试器,我可以看到传递给回调调用函数的值已正确定义为true或false,但是在回调实现中,它变为undefined。
I have tried looking at several other similar questions like this this and this but cannot seem to get this to work. 我试图寻找像其他一些类似的问题, 这 本与此 ,但似乎无法得到这个工作。
This is my plugin definition: 这是我的插件定义:
(function ($) {
$.fn.switcherButton = function (options) {
// Set the default options
var settings = $.extend({},$.fn.switcherButton.defaults, options);
this.click(function () {
$.fn.switcherButton.switched = !$.fn.switcherButton.switched;
settings.onSwitch.call($.fn.switcherButton.switched);
});
return this;
};
// Plugin defaults – added as a property on our plugin function.
$.fn.switcherButton.defaults = {
onSwitch: function() {}
};
$.fn.switcherButton.switched = false;
}(jQuery));
HTML: HTML:
<a id="switchTest" href="#">switch</a>
plugin initialization: 插件初始化:
$("#switchTest").switcherButton({
onSwitch: function(switched){
if(typeof switched === "undefined")
alert("callback param = undefined");
else
if(switched)
alert("callback param = true");
else
alert("callback param = false");
}
});
I have created a jsfiddle of the problem here . 我在这里创建了一个问题的jsfiddle。
Change settings.onSwitch.call($.fn.switcherButton.switched)
to settings.onSwitch($.fn.switcherButton.switched)
将settings.onSwitch.call($.fn.switcherButton.switched)
更改为settings.onSwitch($.fn.switcherButton.switched)
Call is used to set this and not the actual argument to the function 调用用于设置此参数,而不是函数的实际参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.