事件绑定文件上传 [英] Event binding on file upload

查看:90
本文介绍了事件绑定文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法绑定一个事件,当用户选择一个文件上传,并点击打开,我想触发事件,当用户点击打开。



解决方案

在这种情况下,更改事件将被解雇。你有这个HTML:

 < input type =fileid =fileInput/> 

然后使用这个JS:



<$ p $ onchange = function(){
// this.value
};
};

(可以使用 addEventListener / attachEvent 而不是设置 onclick 属性



处理程序,您可以使用 this.value 来获取所选文件。



当然,使用jQuery可以使用:

  $(document).ready(function(){
$(#fileInput)。 on(change,function(){
// this.value OR $(this).val()
});
});

注意: window.onload code>和 $(document).ready 处理程序用于确保元素可用。当然,这个事件发生的时间比实际需要的要晚得多,因为他们等待页面上的所有元素都准备好了( window.onload 被加载)。一个选项是在页面元素之后或< body> onchange >。


Is there a way to bind an event when a user selects a file to upload and clicks on "open", I want to trigger the event when the user has clicked on open.

解决方案

In that scenario, the change event will be fired.

If you have this HTML:

<input type="file" id="fileInput" />

Then use this JS:

window.onload = function () {
    document.getElementById("fileInput").onchange = function () {
        // this.value
    };
};

(with the option of using addEventListener/attachEvent instead of setting the onclick property)

Inside the handler, you can use this.value to get the file selected.

Of course, with jQuery, you can use:

$(document).ready(function () {
    $("#fileInput").on("change", function () {
        // this.value OR $(this).val()
    });
});

NOTE: The window.onload and $(document).ready handlers are used to make sure the element is available. Of course, this event can occur much later than actually necessary, since they wait for all elements on the page to be ready (and window.onload waits even longer for things like images to be loaded). An option is to bind the onchange handler immediately after the element on the page or at the end of the <body>.

这篇关于事件绑定文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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