admin管理员组文章数量:1384812
addEventListener
for input file select in IE 9 and 10 should trigger after the file selection but it triggers after the second time for file select, that means for the first time if no files are selected then for first select it wont trigger and after that for every file selection the listener event triggers (if different file is selected). My code snippet:
HTML
<input type="file" name="imagefile" id="upload">
JavaScript
var file = document.getElementById("upload");
file.addEventListener("change", handlefileselect, false);
function handlefileselect(event) {
alert("file selected");
}
The code runs fine in Firefox and Chrome but has a problem with IE.
addEventListener
for input file select in IE 9 and 10 should trigger after the file selection but it triggers after the second time for file select, that means for the first time if no files are selected then for first select it wont trigger and after that for every file selection the listener event triggers (if different file is selected). My code snippet:
HTML
<input type="file" name="imagefile" id="upload">
JavaScript
var file = document.getElementById("upload");
file.addEventListener("change", handlefileselect, false);
function handlefileselect(event) {
alert("file selected");
}
The code runs fine in Firefox and Chrome but has a problem with IE.
Share Improve this question edited Apr 20, 2013 at 5:17 Ry-♦ 225k56 gold badges493 silver badges499 bronze badges asked Apr 20, 2013 at 5:10 PRASANTHPRASANTH 7053 gold badges15 silver badges33 bronze badges3 Answers
Reset to default 4Old IE versions does not support .addEventListener() method, it has a .attachEvent() method instead to add events to elements.
Use the following addEvent method
function addEvent(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
elem.attachEvent("on"+evnt, func);
}
else { // No much to do
elem[evnt] = func;
}
}
var file = document.getElementById("upload");
addEvent('change', file, handlefileselect)
You should use attachEvent function for IE.
file.addEventListener ? file.addEventListener("change", handlefileselect, false) : file.attachEvent("onchange", handlefileselect);
try to use this, I didn't check but most of the IE problems were resolved with this tag in the header part
<meta http-equiv="X-UA-Compatible" content="IE=edge">
本文标签: javascriptListen to change event in Internet ExplorerStack Overflow
版权声明:本文标题:javascript - Listen to change event in Internet Explorer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535607a2611297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论