从 javascript 调用时触发文件输入的更改事件(即头痛)
我不知道我做错了什么。我已经找到了解决方案/黑客,但它们似乎都不起作用。 尝试使用单个按钮/图像(某物)而不是输入文件控件。到目前为止,除了在 Internet Explorer 中之外,它都有效。在 ie 中,出现“选择文件”对话框,让您选择一个文件,随附的文本框将被填充,但更改事件不会触发。 我尝试过 focus、blur、live、onpropertychange、change、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">Click Me!</a>
<input type="file" id="file" name="file" />
</div>
</form>
</body>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@Pointy 说过了。如果它是一个 ID,则不需要指定标签名称,因此只需执行以下操作:
当您缓存 jQuery 对象时,在变量名称中添加 $(美元符号)也是一个很好的做法,只是这样你知道变量实际上是一个 jQuery 对象。
@Pointy said it. If it's an ID, you don't need to specify the tag name, so just do something like:
It's also a good practice to add a $ (dollar sign) in your variable names when you're caching a jQuery object, just so you know that a variable is actually a jQuery object.
这是 Oppdal 解决方案的文件部分的稍微清晰的实现:
Here's a slightly cleaner implementation of the file part of Oppdal's solution:
它在新的 jQuery 版本中已被更改。我从 1.4.4 更新到 1.6.2,无需解决方法即可正常工作。
It has been changed in the new jQuery version. I updated from 1.4.4 to 1.6.2 and it works fine without the workaround.
知道了! (我认为)。
似乎有效。
Got it! (I think).
Seems to work.