Ajax show hide在IE中不起作用

时间:2013-04-04 12:07:19

标签: javascript ajax

我需要更改什么才能让代码在IE中运行?在IE保存的消息中根本不显示/隐藏。

此脚本在MySQL中自动保存输入。成功保存后,将显示消息“已保存”。它适用于Firefox和Chrome:

<script type="text/javascript">
  function init(){
    window.setInterval(autoSave,15000); // 15000=15 seconds
  }

  function autoSave(){
    var date_day1 = document.getElementById("date_day1").value;
    var amount1 = document.getElementById("amount1").value;
    var params = "date_day1="+date_day1+"&amount1="+amount1;
    var http = getHTTPObject();

    http.onreadystatechange = function(){
      if(http.readyState==4 && http.status==200){
        msg = document.getElementById("msg");
        msg.innerHTML = "Saved";
        $('#msg').show();
      }

      setTimeout(function () {
        $('#msg').fadeOut(function(){
          $(this).hide();
        });
      }, 2000);

    };

    http.open("POST", window.location.href, true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    http.send(params);
  }

  //cross-browser xmlHTTP getter
  function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)

      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
          xmlhttp = false;
        }
      }    

    @else
      xmlhttp = false;
    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
      try {
        xmlhttp = new XMLHttpRequest();
      } catch (e) {
        xmlhttp = false;
      }
    }

    return xmlhttp;
  }
</script>

HTML:

<div id="msg"></div>

0 个答案:

没有答案