Javascript jQuery 从字符串调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3326078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:09:28  来源:igfitidea点击:

jQuery call function from a string

javascriptjquerystringfunctionmethods

提问by Aakash Chakravarthy

Is it possible to call a function by using the strings ?

是否可以使用字符串调用函数?

(i.e) I have a variable var target = 'next';. Using this string I want to call the jquery method next(). Should i use target + '()'(this is bit foolish) to call next()?

(即)我有一个变量var target = 'next';。使用这个字符串我想调用 jquery 方法next()。我应该使用target + '()'(这有点愚蠢)来打电话next()吗?

I know it can be done using conditionalstatements. Since it is a string got from users, but it is difficult to use conditional statements for all that.

我知道可以使用conditional语句来完成。由于它是从用户那里得到的字符串,但是很难使用条件语句来完成所有这些。

In my jQuery plugin, users will pass the value prev, siblingsetc as options, so that the respective jQuery methods will be executed.

在我的jQuery插件,用户将通过价值prevsiblings等等作为选项,使得相应的jQuery方法将被执行。

How do I implement this ?

我该如何实现?

回答by Gumbo

You can use the bracket notationto access the member with a string containing the identifier:

您可以使用括号表示法通过包含标识符的字符串访问成员:

var target = 'next';
$("foobar")[target]();    // identical to $("foobar").next()

回答by iblamefish

If you're wanting to use jQuery, the answer is quite elegant. Because jQuery is an object (which can be accessed like an array) - you can use $("selector")[target]().

如果您想使用 jQuery,答案非常优雅。因为 jQuery 是一个对象(可以像数组一样访问)-您可以使用$("selector")[target]().

Examples:

例子:

var target = 'next';
jQuery("selector")[target]();

This will work if you know that you can trust the input. However, if you're not sure of this, you should check that the function exists before trying to run it otherwise you'll get an error.

如果您知道可以信任输入,这将起作用。但是,如果您不确定这一点,则应在尝试运行该函数之前检查该函数是否存在,否则您将收到错误消息。

var target = 'doesNotExist';
if (jQuery.isFunction(target)) {
  jQuery('selector')[target]();
}

回答by Neo

In my case I needed to get the value from a rel attribute and then parse it as a function, this worked for me.

就我而言,我需要从 rel 属性中获取值,然后将其解析为函数,这对我有用。

$jq('#mainbody form').submit(function(e){
    var formcheck = $jq(this).attr('rel');
    if (typeof window[formcheck] === 'function'){
        formok = window[formcheck]();
        e.preventDefault();
    }
});
function maincheck(){
    alert("Checked");
    return false;
}

and the form

和表格

<div id="mainbody">
<form action="mainpage.php" method="post" rel="maincheck">
<input type="hidden" name="formaction" value="testpost">
<label>Field 1 <input type="text" name="field1" value="<?=$_POST['field1'];?>"></label><br>
<label>Field 2 <input type="text" name="field2" value="<?=$_POST['field2'];?>"></label><br>
<input type="submit" value="Submit Form">
</form>
</div>