IE中的keypressed()和onCLick()实现问题

时间:2010-10-16 12:29:44

标签: php javascript ajax keypress

我有以下输入文字框和一个按钮。

<div id="sender" onKeyUp="keypressed(event);">
     Your message: <input type="text" name="msg" size="70" id="msg" />
     <button onClick="doWork();">Send</button>
</div>

keypressed(event)函数实际检测被按下的键是否为“Enter”并调用doWork()函数。

function keypressed(e){
     if(e.keyCode=='13'){
         doWork();
     }
}

此代码似乎在Chrome&amp; Firefox浏览器。但是在IE中,函数似乎调用了两次。

任何人都可以帮我调整代码,以便它在IE中也能正常工作。

由于

已编辑:

整个ajax代码

<!--
      var httpObject = null;
      var link = "";
      var timerID = 0;
      var nickName = "Unname"; //"<?php echo $nickname; ?>";

      // Get the HTTP Object
      function getHTTPObject(){
         if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
         else if (window.XMLHttpRequest) return new XMLHttpRequest();
         else {
            alert("Your browser does not support AJAX.");
            return null;
         }
      }   

      // Change the value of the outputText field
      function setOutput(){
         if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var objDiv = document.getElementById("result");
            objDiv.innerHTML += response;
            objDiv.scrollTop = objDiv.scrollHeight;
            var inpObj = document.getElementById("msg");
            inpObj.value = "";
            inpObj.focus();
         }
      }

      // Change the value of the outputText field
      function setAll(){
         if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var objDiv = document.getElementById("result");
            objDiv.innerHTML = response;
            objDiv.scrollTop = objDiv.scrollHeight;
         }
      }

      // Implement business logic    
      function doWork(){    
         httpObject = getHTTPObject();
         if (httpObject != null) {
            link = "message.php?nick="+nickName+"&msg="+encodeURIComponent(document.getElementById('msg').value);
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setOutput;
            httpObject.send(null);
         }
      }

      // Implement business logic    
      function doReload(){    
         httpObject = getHTTPObject();
         var randomnumber=Math.floor(Math.random()*10000);
         if (httpObject != null) {
            link = "message.php?all=1&rnd="+randomnumber;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setAll;
            httpObject.send(null);
         }
      }

      function UpdateTimer() {
         doReload();   
         timerID = setTimeout("UpdateTimer()", 5000);
      }


      function keypressed(e){
         if(e.keyCode=='13'){
            doWork();
         }
      }
    //-->

1 个答案:

答案 0 :(得分:0)

如果有人遇到类似问题,这可能会有用。 我通过使用PHP脚本检测IE浏览器并在为IE呈现HTML时丢弃onKeyUp()函数来解决问题。

IE Detection PHP脚本

//Detect IE function
    function detectIE() {
        if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
        return true;
        else
        return false;
    }

这就是我改变事件处理的方式。

<div id="sender" <?php if (detectIE()) { /*do nothing */ } else { echo "onKeyUp=\"keypressed(event);\""; } ?> >

 Your message: <input type="text" name="msg" size="70" id="msg" />
 <button onClick="doWork();">Send</button>
 </div>