1

这是我的HTML,

<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 键时,它的浏览器只闪烁,没有任何反应。有趣的是,它不执行 alert("1");

搜索输入()代码:-

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;
           }
        }
     }
  }

请帮忙....

4

2 回答 2

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>
于 2013-10-14T08:23:42.110 回答
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() 中提到的处理程序覆盖事件处理程序,因此下次执行按键时,警报不会发生。这有意义吗?

于 2013-10-14T07:54:40.293 回答