Opera中的输入类型文件 [英] input type file in Opera
问题描述
我尝试在文件输入上触发onkeydown,onkeypress和onkeyup事件(例如,当元素处于焦点状态并且按下某个键时,不会触发事件),但是它们在Opera中不起作用.用jQuery触发'click'也不起作用(例如$('#myFileinput').Click()或$('#myFileinput').trigger('click')).
I have tried to fire onkeydown, onkeypress and onkeyup events on file input(e.g. when the element is on focus and a key is pressed the events are not fired), but they doesn't work in Opera. Firing 'click' with jQuery , doesn't work too (e.g. $('#myFileinput').Click() or $('#myFileinput').trigger('click')).
是否可以在Opera中触发这些事件?
Is there a way to trigger these events in Opera ?
这是我的代码:
<input type="file" class="foo" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript">
debugger;
window.onload = onPageLoad;
function onPageLoad() {
var input = $(".foo");
debugger;
input.keydown(function () {
//doesn't work in Opera
alert("keydown");
});
input.keypress(function () {
//doesn't work in Opera
alert("keypress");
});
input.keyup(function () {
//doesn't work in Opera
alert("keyup");
})
}
window.onkeydown = function () {
//when the focus is on the input the code bellow doesn't fire
alert("window key down")
}
</script>
推荐答案
由于安全策略的缘故,Opera在输入type = file上触发的事件受到限制.另外,在新的Opera版本中,专注于输入的内容会弹出一个文件对话框,因此无论如何都不会触发任何键事件.如果您需要知道值何时更改,请侦听change事件.
Due to security policies, it is limited what events Opera fires on an input type=file. Also, in new Opera versions focusing the input will bring up a file dialog, so no key events would ever fire on the input anyway. If you need to know when the value changes, listen for the change event.
这篇关于Opera中的输入类型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!