Call a JavaScript function name using a string?

前端 未结 10 1058
孤独总比滥情好
孤独总比滥情好 2020-11-30 07:07

How can I hook up an event to a function name I have defined as a string?

I\'m using Prototype.js, although this is not Prototype-speficic.

$(inputId         


        
相关标签:
10条回答
  • 2020-11-30 07:43

    Do you know what the onclick property contains or what type it is? I assume this is prototype specific stuff, as "fields" does not exist in DOM forms.

    0 讨论(0)
  • 2020-11-30 07:51

    Perhaps?

    setTimeout ( "myFunc()", 1 );
    
    0 讨论(0)
  • 2020-11-30 07:59

    Just an eval would do the job

    var call = eval("method_name").call(args);
    
    0 讨论(0)
  • 2020-11-30 07:59

    Looks like formData.fields[x].onclick holds the name of a global function? If so try:

    $(inputId).observe('click', window[formData.fields[x].onclick]);
    
    0 讨论(0)
提交回复
热议问题