如何为文件字段设置监听器 - 当用户选择文件时,采取行动
我有文件上传字段。当用户选择文件时,会在下面添加一个新字段,允许用户选择另一个文件等。
$("#photos").change(function(){
var countEmpty = 0;
$("#photos input").each(function(index) {
if ( $(this).val() == '' )
countEmpty++;
});
if (countEmpty == 0)
addNewPhoto();
});
这就是现在的代码。它实际上监听包含输入字段的 div 中的更改。当它被选择、单击或执行其他操作时,脚本会检查是否有空字段。如果没有空字段,则会添加一个新字段。
但这在IE8中存在问题。在 Firefox/Chrome 中,当用户选择文件时,新字段会立即出现。但在 IE 中,用户必须单击空格、滚动或其他操作 - 在下一个字段出现之前。
有什么建议如何修复 IE 吗?
谢谢。
PS:我已经看到了 uploadifier,但目前无法在我的项目中实现它。
I have file upload field. When user selects file, a new field is added right underneath, allowing user to select another file, etc.
$("#photos").change(function(){
var countEmpty = 0;
$("#photos input").each(function(index) {
if ( $(this).val() == '' )
countEmpty++;
});
if (countEmpty == 0)
addNewPhoto();
});
That's the code right now. It listens actually to a change in the div that contains the input fields. When it gets selected, clicked, or whatever, the script checks for empty fields. If no empty fields, it adds a new one.
But this has problem in IE8. In Firefox/Chrome, when user select file, new field appears right away. But in IE, user has to click whitespace, or scroll, or something - before the next field appear.
Any suggestions how to fix for IE?
Thanks.
PS: I have seen uploadifier but wouldn't be able to implement it for my project at this time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与 IE 的
onChange
实现有关。在 IE 中,输入元素必须失去焦点,如果该输入的值发生更改,则将触发onChange
。 (顺便说一句,看看onChange
的 HTML 4 定义,IE 实际上做得正确,而 Firefox 做错了 http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-onchange)我建议将您的事件从
onChange
更改为其他事件,例如onClick
。This is to do with IEs implementation of
onChange
. In IE the input element must loose focus, if the value of that input has changed theonChange
will then be fired. (btw look at the HTML 4 definition ofonChange
, IE actualy does it right where as Firefox... do it wrong http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-onchange)I would suggest changing your event from
onChange
to a different event e.g.onClick
.