通过内联数据属性传递 Javascript 函数 [英] Passing a Javascript function through inline data- attributes

查看:22
本文介绍了通过内联数据属性传递 Javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Javascript 文件上传库,其中一项功能是它使用 HTML5 内联数据属性将信息传递给插件.

I'm working with a Javascript file upload library, and one of it's features is that it uses HTML5 inline data- attributes to pass information to the plugin.

这对于任何与数据相关的数据、字符串、数字等都非常有用,但是该插件有一些回调方法,您可以为其分配函数.我的问题是,当尝试通过这些内联数据属性传递 javascript 函数时,如下所示:

This is working great for anything data related, strings, numbers etc., however the plugin has some callback methods you can assign function to. My problem is that when trying to pass a javascript function through these inline data attributes, like this:

<input type="file" name="test" data-on-finish="alert();">

该插件可以很好地获取对 onFinish() 回调方法的引用,但是当它尝试执行我放入的任何 javascript 时,出现错误:

The plugin picks up the reference to the onFinish() callback method fine, but when it tries to execute whatever javascript I put in there I get the error:

Uncaught TypeError: Object alert(); has no method 'call' 

我假设它正在将 alert(); 作为字符串读取.知道如何将可执行的 javascript 传递给插件吗?

I'm assuming it's reading the alert(); as a string. Any idea how I can pass through executable javascript to the plugin?

我相信我使用的插件是 jQuery 文件上传插件的扩展:https://github.com/blueimp/jQuery-File-Upload/wiki/Options

I believe the plugin I'm using is an extension to the jQuery file upload plugin: https://github.com/blueimp/jQuery-File-Upload/wiki/Options

更新:我也试过使用全局定义的函数,像这样:

Update: I've also tried using globally defined functions, like this:

<script type="text/javascript">
    function myTesting(){
       alert('yay');
    }
</script>
<input type="file" name="test" data-on-finish="myTesting">

我已经尝试将 data-on-finish 属性更改为 myTestingmyTesting(),但仍然没有运气...

I've tried changing the data-on-finish attribute to myTesting, myTesting(), still had no luck...

推荐答案

为什么不放置回调的名称呢?类似的东西

why not place the name of the callback instead? something like

//for example, a global function
window['someFunctionName'].call();

//a namespaced function
//similar to doing ns.someFunctionName()
ns['someFunctionName'].call();

这篇关于通过内联数据属性传递 Javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆