1

I am having a surprisingly hard time trying to do this. Any help would be much appreciated.

what in trying to do is i have a bunch of functions and if "funtion1" is typed into the input feild and submited the function1 will run. if "function2" is submitted, function2 will run. thought is would be simple but i can't seem to get it.

4

3 回答 3

4

您可以使用函数的对象数组,例如:

var funcs = {
    'func1': function() {},
    'func2': function() {},
    'func3': function() {},
    'func4': function() {}
};
$(function() {
    $('a.run').click(function() {
        var f = funcs[$('#myinput').val()];
        if(f) f();
        return false;
    });
});
于 2013-09-11T22:31:36.697 回答
2

你可能想使用一个switch语句

switch(document.getElementById("myinput").value) {
  case 'function1':
       // do something
  break;
  case 'function2':
       // do something else
  break;
}
于 2013-09-11T22:30:21.833 回答
0
var functions = {
    test : function(){alert("test");}
};

$("#click").click(function () {
    var func = $("#run").val();
    console.log(functions[func] != undefined);
    if (functions[func] != undefined) {
        functions[func]();
    }
});

小提琴

于 2013-09-11T22:48:59.037 回答