2013-02-03 40 views
0

为什么不能在ie中使用onChange? 这是否有解决方案?在Internet Explorer中更改

HTML:

<select id="auto_doors" style="display:none;" name="auto_doors" onchange="updateField(this.value, 'auto_fuel', 5, 6, this.parentNode.id), resetBelow(4,'auto'), show('auto_fuel')"> 
</select> 

功能:

if (jQuery.browser.msie) { setTimeout(DoSomething, 0); } else { DoSomething(); } 
     function updateField(str, id, prevvalue, value, vehicletype) 
     { 
     if (str=="") 
      { 
      document.getElementById(id).innerHTML=""; 
      return; 
      } 
     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
      } 
     else 
      {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
      document.getElementById(id).innerHTML=xmlhttp.responseText; 
      } 
      } 
     xmlhttp.open("GET","inc/form_rest.php?q="+str+"&prevvalue="+prevvalue+"&value="+value+"&vehicletype="+vehicletype,true); 
     xmlhttp.send(); 
     } 
+4

如果你使用的是jQuery,那么你为什么要做'window.XMLHttpRequest'?哦,你可以在IE中使用onchange。你有什么问题/错误? – j08691

+1

是的,如果你无论如何背负着一个图书馆的重量,你最好尽可能地使用它。 – techfoobar

+0

jQuery ajax doc http://api.jquery.com/jQuery.ajax/ –

回答

0

三点意见,可以帮助:

  1. 没有必要支持IE6及以下了。 new XMLHttpRequest()就足够了。
  2. 您应该在之后设置onreadystatechange呼叫.open()。在某些浏览器(probaby只是IE)中调用.open()会算作“新请求”并清除readystatechange处理程序。
  3. 旧版本的IE(IE7?)不喜欢this.value<select>上。相反,你应该使用this.options[this.selectedIndex].value
1

尝试更改事件绑定到auto_doors元素像这样代替:

$("#auto_doors").change(function(){ 
    updateField(this.value, 'auto_fuel', 5, 6, this.parentNode.id), resetBelow(4,'auto'), show('auto_fuel'); 
}); 
+0

嗯,这是非常令人沮丧的,我认为我现在必须改变很多代码.. –

+0

这仍然不会发送值到下一个选择框,它只是使它出现,但它仍然在其他浏览器如chrome和ff –

1

您可以使用jQuery来解决这个问题的代码将是这样的:

$('#auto_doors').change(function() { 
    alert('Handler for .change() called.'); 
});