我正在编写一个插件,我需要“绑定(bind)”参数。 有时我需要 this.checked 的值,但有时我需要 $(this).val() ,并且当我需要检查其他属性时存在极端情况。
我需要一个用于初始化的“输入参数”,它以某种方式确定要检查值的内容。
我可以用 eval 做到这一点,但这是邪恶的。 我可以用开关盒做到这一点,但我不确定我是否能做好一切准备。
最好的方法是什么?
$.fn.formSwitch = function (options) {
var settings = $.extend({
visibleholder: '#visible_holder',
hiddenholder: '#hidden_holder',
reparseform: this.closest('form')
}, options);
this.change(function () {
switchFormPart(settings.visibleholder,
settings.hiddenholder,
********this.checked, *********** <- I would like this as a parameter
settings.reparseform
);
});
};
最佳答案
您可以传递诸如 "checked"
或 "val"
之类的字符串作为参数,然后:
this.change(function () {
switchFormPart(settings.visibleholder,
settings.hiddenholder,
param in this ? this[param] : $(this)[param](),
settings.reparseform
);
});
如果参数是“checked”
,它将执行相当于this.checked
如果参数是"val"
,它的作用相当于$(this).val()
等等。
关于javascript - 可变属性作为 jQuery 插件中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11031236/