如何在JavaScript函数中传递file元素
我在我的应用程序中使用JavaScript的函数名裁剪器就是这样如何在JavaScript函数中传递file元素,javascript,jquery,html,Javascript,Jquery,Html,我在我的应用程序中使用JavaScript的函数名裁剪器就是这样 function cropper(){ var selectedImg = $('#image_file')[0].files[0]; } 我使用ID为image\u的file元素从html中调用它,如下所示 <input type="file" id="image_file" name="picture1" onchange="cropper()"/><br> 这样我就可以为不同的文件元素分配不同的
function cropper(){
var selectedImg = $('#image_file')[0].files[0];
}
我使用ID为image\u的file元素从html中调用它,如下所示
<input type="file" id="image_file" name="picture1" onchange="cropper()"/><br>
这样我就可以为不同的文件元素分配不同的ID。您能建议我如何实现上述功能吗
编辑:
我有4个文件附件按钮在我的网站,我想使用不同的ID,所以它会像这样
<input type="file" id="picture1" name="picture1" onchange="cropper(picture1)"/><br>
<input type="file" id="picture2" name="picture2" onchange="cropper(picture2)"/><br>
<input type="file" id="picture3" name="picture3" onchange="cropper(picture3)"/><br>
<input type="file" id="picture4" name="picture4" onchange="cropper(picture4)"/><br>
您可以将事件对象传递给处理程序
<input type="file" id="image_file" name="picture1"
onchange="cropper(event)"/><br>
我需要对我的文件处理程序使用不同的ID
<input type="file" id="image_file" name="picture1"
onchange="cropper(event)"/><br>
function cropper(event){
var selectedImg = event.target.files ? event.target.files[0]
: $('#image_file')[0].files[0];
}
//variable = id of an element.
function cropper(variable){
var selectedImg = $('#'+variable)[0].files[0];
}