OnClick 事件在 Chrome 中不起作用

OnClick event doesn't work in Chrome

我写了下面的代码在我的页面上有一个 select 列表:

<select id='defchtype' class='selectpicker form-control' style='width:80%;'>
    <option id='Pie' onClick='changechart(".$_GET['id'].",\"Pie\")'>Pie</option>
    <option id='Line' onClick='changechart(".$_GET['id'].",\"Line\")'>Line</option>
    <option id='Bar' onClick='changechart(".$_GET['id'].",\"Bar\")'>Bar</option>
    <option id='Odometer' onClick='changechart(".$_GET['id'].",\"Odometer\")'>Odometer</option>
    <option id='Radar' onClick='changechart(".$_GET['id'].",\"Radar\")'>Radar</option>
</select>

当我使用 mozilla firefox 浏览器时,这是正确的。但是当我使用 google chrome 浏览器时,onclick 事件(changechart 函数)不会执行。我该如何解决?谢谢 这是我呈现的 html 代码的一部分:

<select id='defchtype' class='selectpicker form-control' style='width:80%;'>
        <option id='Pie' onClick='changechart(126,"Pie")'>Pie</option>
        <option id='Line' onClick='changechart(126,"Line")'>Line</option>
        <option id='Bar' onClick='changechart(126,"Bar")'>Bar</option>
        <option id='Odometer' onClick='changechart(126,"Odometer")'>Odometer</option>
        <option id='Radar' onClick='changechart(126,"Radar")'>Radar</option>
</select>

这是我的 javascript 功能:

function changechart(id,chtype){
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
          xmlhttp=new XMLHttpRequest();
      }
      else
      {
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
              //response="server/img/useravatars/"+xmlhttp.responseText;
              response=xmlhttp.responseText;


            document.getElementById("chartcontainer").innerHTML=response;
              document.getElementById(chtype).selected="true";

          }
    }
    var req="showchart.php?id="+id+"&chtype="+chtype;
    xmlhttp.open("GET",req,true);
    xmlhttp.send(); 
}

javascript 函数甚至不在 chrome 中执行。但在 Firefox 中工作正常

您不应在 option 元素中使用 onClick 事件。使用 select 的 onchange 事件来代替这个问题中已经讨论过的:javascript onclick alert not working in chrome .