jQuery动态绑定文件输入并在函数中检索文件

jQuery bind file input dynamically and retrieve file in a function

本文关键字:文件 函数 检索 动态绑定 输入 jQuery      更新时间:2023-09-26

我有一个函数将创建一个文件输入字段,例如

function addCol () {
   //add file input fields
   //last line of the function will bind all file input fields to a function
   $('#upload_'+rowNum[1]+'_'+colSize).bind('change', {fh:'upload_'+rowNum[1]+'_'+colSize}, readURL);
}

功能

function readURL(e) {
    alert(e.data.fh);
    var input = $('#'+e.data.fh);
    alert(input.files);
}

因此,

e.data.fh = 'upload_1_1'
input.files = undefined

有人知道我如何从文件输入字段中动态检索文件吗?

尝试e.target.files[0]e.target返回html标记<input type="file">,在该标记中有一个属性调用files,其中包含已加载的所有文件