在 IE 中使用 javascript 以编程方式触发组合框的 onchange 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7647901/
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
Fire onchange for combo box programatically using javascript in IE not working
提问by Ved
I want to call onchange function dynamically when user change value of another combo box. My code is as under :
当用户更改另一个组合框的值时,我想动态调用 onchange 函数。我的代码如下:
HTML
HTML
<select name="vbitratecontrol0" id="combo1" onchange="set()">;
<option value="0">None</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
Javascript
Javascript
function fun(){
document.getElementById(combo1).onChange();
}
when function fun is called, onchange of combo1 should be fired which is working fine in FF but not in IE6,7,8. fun I m calling from another method.
当函数 fun 被调用时,combo1 的 onchange 应该被触发,这在 FF 中工作正常,但在 IE6、7、8 中无效。有趣的是我从另一种方法调用。
Please help... Thanks
请帮助...谢谢
回答by
<select name="vbitratecontrol0" id="combo1" onchange="set()">;
<option value="0">None</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
<script>
function fireEvent(element,event){
if (document.createEventObject){
// dispatch for IE
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt)
}
else{
// dispatch for firefox + others
var evt = document.createEvent("Events");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
}
function set(){alert("asd");}
function fun(){
fireEvent(document.getElementById("combo1"), "change");;
}
fun();
</script>
HTH!
哼!