我正在使用Javascript文件上传库,其中一个功能是它使用HTML5内联数据属性将信息传递给插件。
这适用于任何数据相关,字符串,数字等,但插件有一些可以分配函数的回调方法。我的问题是,当试图通过这些内联数据属性传递javascript函数时,如下所示:
<input type="file" name="test" data-on-finish="alert();">
该插件可以很好地获取对onFinish()回调方法的引用,但当它尝试执行我放入的任何javascript时,我收到错误:
Uncaught TypeError: Object alert(); has no method 'call'
我假设它正在以字符串形式读取alert();
。知道如何将可执行的javascript传递给插件吗?
我相信我使用的插件是jQuery文件上传插件的扩展: https://github.com/blueimp/jQuery-File-Upload/wiki/Options
更新 我也尝试使用全局定义的函数,如:
<script type="text/javascript">
function myTesting(){
alert('yay');
}
</script>
<input type="file" name="test" data-on-finish="myTesting">
我尝试将data-on-finish
属性更改为myTesting
,myTesting()
,仍然没有运气......
答案 0 :(得分:20)
为什么不放置回调的名称呢?
之类的东西//for example, a global function
window['someFunctionName'].call();
//a namespaced function
//similar to doing ns.someFunctionName()
ns['someFunctionName'].call();
答案 1 :(得分:1)
使用类似的东西:
$(function() {
function test() {
alert("ok");
}
$(".test").data("test", test);
$(".test").data("test")();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="test">
</div>
&#13;
答案 2 :(得分:0)
看起来它正试图回调函数,如myfunction.call(
尝试传递就是这样的函数。
<input type="file" name="test" data-on-finish="myFunction;">
function myFunction(){
alert('I am alerting');
};