2013-10-14 77 views
1

這裏是我的HTML工作,輸入框的事件,如聚焦狀態,的onblur,onkeypress事件不會在Chrome

<input id="searchval" type="text" class="search-query" value="People, Links" onfocus = "searchtext();" onblur = "searchetextin();" onkeypress = "Searchinput();" placeholder="People, Links">

這是我的網站上搜索文本,當用戶按下回車鍵,我會觸發代理來打印搜索結果。

onkeypress事件用於在數據庫中搜索結果並打印它們。但它不適用於Chrome。結果在FF和IE上打印效果良好。

當我按下Enter鍵時,瀏覽器只閃爍,沒有任何反應。有趣的是,它不執行警報(「1」);

Searchinput()代碼: -

function Searchinput() { 
    alert("1"); 
    document.getElementById('searchval').onkeypress = function(e) { 
     if (!e) e = window.event;  
     if (e.keyCode == '13') { 
      var search = document.getElementById("searchval").value; 
      //alert(window.parent.frames.length); 
      if (search == "") { 
      alert("Search field can not be blank"); 
      return false; 
      } 
      if (window.parent.frames.length == 3) { 
      var user = document.forms[0].UserName.value; 
      parent.frames[2].location = "aSearchResults?OpenAgent&" + search +"~"+user; 
      return false; 
      } else { 
      var user = document.forms[0].UserName.value; 
      parent.frames[1].location = "aSearchResults?OpenAgent&" + search+"~"+user; 
      return false; 
      } 
     } 
    } 
    } 

請幫助....

+0

你檢查控制檯中鉻的開發者工具的錯誤 – Mozak

+0

有沒有錯誤,我檢查了它的功能工作。 –

回答

0

我已經編輯了一點點你的代碼(去掉調用其定義你沒有共享功能)

<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <input id="searchval" type="text" class="search-query" value="People, Links" 
     onkeypress="Searchinput();" placeholder="People, Links"> 
    <script type="text/javascript"> 
     function Searchinput() { 
      alert("1"); 
      document.getElementById('searchval').onkeypress = function (e) { 
       if (!e) e = window.event; 
       if (e.keyCode == '13') { 
        var search = document.getElementById("searchval").value; 
        //alert(window.parent.frames.length); 
        if (search == "") { 
         alert("Search field can not be blank"); 
         return false; 
        } 
        if (window.parent.frames.length == 3) { 
         var user = document.forms[0].UserName.value; 
         parent.frames[2].location = "aSearchResults?OpenAgent&" + search + "~" + user; 
         return false; 
        } else { 
         var user = document.forms[0].UserName.value; 
         parent.frames[1].location = "aSearchResults?OpenAgent&" + search + "~" + user; 
         return false; 
        } 
       } 
      } 
     } 
    </script> 
</body> 
</html> 

上按下一個鍵的第一次,警報情況,然後將其覆蓋與您在Searchinput(提到處理程序的事件處理程序),因此下一次你執行一個關鍵前ss,警報不會發生。 這有道理嗎?

1

我不知道爲什麼和怎樣,但如果你把它定義爲變量

<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <input id="searchval" type="text" class="search-query" 
     onkeypress="Searchinput();" placeholder="People, Links"> 
    <script type="text/javascript"> 
     Searchinput = function() { 
      alert("1"); 
      document.getElementById('searchval').onkeypress = function (e) { 
       if (!e) e = window.event; 
       if (e.keyCode == '13') { 
        var search = document.getElementById("searchval").value; 
        //alert(window.parent.frames.length); 
        if (search == "") { 
         alert("Search field can not be blank"); 
         return false; 
        } 
        if (window.parent.frames.length == 3) { 
         var user = document.forms[0].UserName.value; 
         parent.frames[2].location = "aSearchResults?OpenAgent&" + search + "~" + user; 
         return false; 
        } else { 
         var user = document.forms[0].UserName.value; 
         parent.frames[1].location = "aSearchResults?OpenAgent&" + search + "~" + user; 
         return false; 
        } 
       } 
      } 
     } 
    </script> 
</body> 
</html>