weixin_33739523 2018-05-24 02:11 采纳率: 0%
浏览 59

未触发onchange方法

    <html>
     <head>
      <script>
       function showUser(str) {
       if (str == "") {
         document.getElementById("txtHint").innerHTML = "";
         return;
       } else { 
        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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
      }
    }
    </script>
    </head>
    <body>

    <form>
     <select name="users" onchange="showUser(this.value)">
     <option value="">Select a person:</option>
     <option value="1">Peter Griffin</option>
     <option value="2">Lois Griffin</option>
     <option value="3">Joseph Swanson</option>
     <option value="4">Glenn Quagmire</option>
     </select>
    </form>
     <br>
     <div id="txtHint"><b>Person info will be listed here...</b></div>

    </body>
    </html>

The page shows the dropdown list. However, it seems the onchange method is not triggered. I don't get any return from the php. The php code is here. https://www.w3schools.com/php/php_ajax_database.asp

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33709219 2018-05-24 02:23
    关注

    It seems to be working for me. Are you running this html file off your local machine? What I mean is that when you are loading this html file on your browser, does the address bar read file://<path_to_html> or http(s)://<path_to_html>?

    If it is the former, then you need to provide the full path to the server that's hosting the getuser.php.

    When I tried your code (running the html off my local machine), the onchange event was being triggered and the xmlhttp object was being created but no http request was being sent to the server (as per what I saw on by browser's dev tools). Then I realized I had to change the location of the getuser.php because it needed to be hosted on a server.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?