这是我的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键时,浏览器只闪烁,没有任何反应。有趣的是,它没有执行警报(“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 :(得分: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>
答案 1 :(得分: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()中提到的处理程序覆盖事件处理程序,因此下次执行按键时,警报不会发生。 这有道理吗?