从javascript调用文件输入时触发更改事件(即头痛) [英] Triggering change event on file input when invoked from javascript (ie headache)
本文介绍了从javascript调用文件输入时触发更改事件(即头痛)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图使用一个按钮/图像(东西),而不是输入文件控制。它工作到目前为止,除了在Internet Explorer中。即在选择文件对话框出现,让你选择一个文件,随附的文本框被填充,但变化事件不会触发。
我尝试过重点,模糊,生活,onpropertychange,改变,onchange ...但他们只是不会工作。任何帮助吗?
jQuery:
$(function(){
var a = $('a。#LinkUpload');
var f = $('input。#file');
a.click(function(){
f.click();
});
f.change(function(){
alert('changed!');
});
}) ;
html:
< body>
< form action =>
< div>
< a id =LinkUpload>点击我!< / a>
< input type =fileid =filename =file/>
< / div>
< / form>
< / body>
解决方案
明白了! (我想)。
$(function(){
var a = $('#LinkUpload');
var f = $('#file');
a.click(function(){
f.click();
setTimeout(function(){$ b $ ($ .browser.msie&& $ .browser.version< b.f 9){
f.bind('propertychange',function(){
alert('changed');
});
} else {
f。 (function(){
alert('changed!');
});
}
});
似乎有效。
I don't know what I'm doing wrong. I've found fixes/hacks for this but none of them seem to be working. Trying to use a single button/image (something) instead of the input-file control. It works so far except for in internet explorer. In ie the 'select file' dialog appears, lets you choose a file, the accompanying textbox gets populated but the change event doesn't fire. I've tried focus, blur, live, onpropertychange, change, onchange... but they just won't work. Any help?
jQuery:
$(function() {
var a = $('a.#LinkUpload');
var f = $('input.#file');
a.click(function() {
f.click();
});
f.change(function() {
alert('changed!');
});
});
html:
<body>
<form action="">
<div>
<a id="LinkUpload">Click Me!</a>
<input type="file" id="file" name="file" />
</div>
</form>
</body>
解决方案
Got it! (I think).
$(function() {
var a = $('#LinkUpload');
var f = $('#file');
a.click(function() {
f.click();
setTimeout(function() {
f.val(f.val());
}, 1);
});
if ($.browser.msie && $.browser.version < 9) {
f.bind('propertychange', function() {
alert('changed');
});
} else {
f.change(function() {
alert('changed!');
});
}
});
Seems to work.
这篇关于从javascript调用文件输入时触发更改事件(即头痛)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文