简体   繁体   English

根据变量名称调用jQuery自动完成小部件函数

[英]Calling a jQuery autocomplete widget function based on variable name

So to initialize autocomplete for a textbox, you do this $('#textbox').autocomplete({...}); 因此,要初始化文本框的自动完成功能,请执行此$('#textbox').autocomplete({...});

I have a custom widget declared: 我有一个自定义的小部件声明:

$.widget( "custom.catcomplete", jQuery.ui.autocomplete, {
    ......
});

In this case the widget's name is catcomplete , so to initialize a custom autocomplete, you can do this: $('#textbox').catcomplete({...}); 在这种情况下,小部件的名称为catcomplete ,因此要初始化自定义自动完成功能,可以执行以下操作: $('#textbox').catcomplete({...});

Now comes the tricky part. 现在是棘手的部分。 I'm trying to call $('#textbox').catcomplete({...}); 我正在尝试调用$('#textbox').catcomplete({...}); using a variable name, in this way: 使用变量名,通过这种方式:

var w = 'catcomplete';
//this is where i try to call the widget
$('#textbox').{w}({...}); // <-- this is obviously wrong

While the example is obviously wrong, but I hope it gives a generally good idea of what I'm trying to do. 尽管该示例显然是错误的,但是我希望它能大致了解我要尝试执行的操作。 By the way the widget cannot be modified (legacy issues). 顺便说一句,小部件无法修改(旧版问题)。

Thanks in advanced! 提前致谢!

This simple code works to call jQuery.find method (get the first div inside the body): 这个简单的代码可以调用jQuery.find方法(获取主体内的第一个div):

$('body')['find']('div:first')

So, the solution for you should be something like this: 因此,您的解决方案应该是这样的:

$('body')[yourVariable]({...})

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

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