onchange with alert not working in ie

最后都变了- 提交于 2019-12-23 17:43:43

问题


javascript based tag ( type ='file' ) created

and add one attribute in that tag

that attribute name onchange, i will assign alert

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

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

回答1:


You can do two ways,

1.. Using HTML, add onchange event inline

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

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

2.. Using JS,

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

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




回答2:


Try with this:

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



回答3:


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




回答4:


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

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 );
}



回答5:


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



来源:https://stackoverflow.com/questions/13701820/onchange-with-alert-not-working-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!