javascript 带有警报的 onchange 在 ie 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13701820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:32:13  来源:igfitidea点击:

onchange with alert not working in ie

javascriptfileinternet-explorertagsonchange

提问by sabari

javascript based tag ( type ='file') created

type ='file'创建基于 javascript 的标签 ( )

and add one attribute in that tag

并在该标签中添加一个属性

that attribute name onchange, i will assign alert

该属性名称onchange,我将分配警报

But alert is not come when choice the new file in internet explore.

但是在 Internet Explorer 中选择新文件时不会出现警报。

choicefile.setAttribute("onChange", "alert('test')");

回答by Muthu Kumaran

You can do two ways,

你可以做两种方法,

1.. Using HTML, add onchangeevent inline

1.. 使用HTML,添加onchange事件内联

<input type="file" id="file_select" name="file_select" value="" onchange="alert('File selected')" />

Demo: http://jsfiddle.net/CS3xJ/1/

演示:http: //jsfiddle.net/CS3xJ/1/

2.. Using JS,

2..使用JS,

  choicefile.onchange = function(){
     alert('File selected')
  }

Demo: http://jsfiddle.net/CS3xJ/2/

演示:http: //jsfiddle.net/CS3xJ/2/

回答by Gurmeet Khalsa

try onclick="javascript:alert('test');" instead of onchange. Old ie versions and compatibility modes don't support onchange very well.

试试 onclick="javascript:alert('test');" 而不是 onchange。旧的 ie 版本和兼容模式不能很好地支持 onchange。

回答by MaxArt

Try with this:

试试这个:

choicefile.onchange = function() {alert("test");};

回答by Akhil Sekharan

Your code seems correct. Something particular with IE is, if you put higher security level, you need to allow scripts and activeXcontent when you load the website.

你的代码似乎是正确的。IE 的特殊之处在于,如果您设置更高的安全级别,您需要在加载网站时允许脚本和 activeX内容。

回答by Fenton

There is actually a difference between setAttributeand attachEvent. Here is an example using attachEvent(for IE) and addEventListener(standards) to add the event.

setAttribute和之间实际上是有区别的attachEvent。这是使用attachEvent(对于 IE)和addEventListener(标准)添加事件的示例。

Also, not that the event handler is a function, rather than a string:

此外,不是事件处理程序是一个函数,而不是一个字符串:

var eventHandler = function () {
    alert("Test");
}

if (choicefile.addEventListener) {
  choicefile.addEventListener('change', eventHandler , false);
} else if (choicefile.attachEvent)  {
  choicefile.attachEvent('onchange', eventHandler );
}