添加参数时未调用回调

callback not getting called when adding arguments

本文关键字:调用 回调 参数 添加      更新时间:2023-09-26

我有一个javascript回调,当传递了参数时,它不会被调用。这在jsbin中是有效的,那么它在phonegap中不起作用有什么原因吗。这应该非常简单。

<input type="file" id="soundInput">
<script type="text/javascript">
var type = 0;
function addSoundToSoundMenu() {
    alert("callback success.");
}
var soundInput = document.getElementById('soundInput');
soundInput.addEventListener('change', function(e) {
    handleFileSelect(e, type,addSoundToSoundMenu);
}, false); 
function handleFileSelect(event, type, cb) {
    var file = this.files[0]; // FileList object
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
        gotFS(fs,file,type);
    }, fail);
}
</script>

上面的例子没有调用handleFileSelect()。只有在传递handleFileSelect时不带任何参数,它才有效。

soundInput.addEventListener('change',handleFileSelect,false); 

当然,我删除了handleFileSelect()的参数以使其工作。

我有什么东西不见了吗。以编程方式创建输入字段是否会更改事件调度程序的处理方式。

还有什么我可以尝试的吗,也许是一些收尾的东西?以前有人对此有意见吗?我无法想象这与phoneGap有任何关系。

您没有将事件对象传递给handleFileSelect函数。请你看看。

soundInput.addEventListener('change', function(e) {
    handleFileSelect(e, type,addSoundToSoundMenu);
}, false); 

感谢您的帮助,我发现了问题,只是为了结束问题,问题是

var file = this.files[0]; // FileList object

在handleFileSelect函数中。如果handlFileSelect不是事件的直接回调,则"this"不包含files对象。

哦,没有调试器编程的乐趣。。。

诅咒你,威尔!