提交/转到专注于输入并按下输入时
问题描述:
我不知道为什么这不起作用?我尝试了几种方法并环顾四周,但没有一个例子适用于我。我的代码:提交/转到专注于输入并按下输入时
HTML
<div id='SearchBar'>
<form id='SearchBar'>
<input placeholder='Search' id='SearchBar'></input>
</form>
</div>
CSS
#SearchBar{
background:#ffffff;
width:224px;
height:32px;
float:right;
}
form#SearchBar {
width:100%;
height:100%;
}
input#SearchBar {
font-size:16px;
border:none;
width:208px;
height:32px;
padding:0 8px;
}
input#SearchBar:focus{
outline-color:#2c5aa0;
}
jQuery的
$('input#SearchBar').keypress(function(event) {
if (event.which == 13) {
window.location = list.html; //or submit or do whatever
}
});
任何帮助表示赞赏。我哪里错了?
答
听keyup
事件,你缺少引号为list.html
,list
对象是undefined
因此它不具有html
财产!
$(function(){
$('#SearchBar').keyup(function(event) {
if (event.which == 13) {
window.location.href = 'list.html';
}
});
})
我不知道为什么,但仍然不适合我...让我加我的HTML,也许这将有所帮助? – jstacks 2013-02-24 04:44:16
@jstacks ID必须是唯一的,jQuery只会选择具有该特定ID的第一个元素。 – undefined 2013-02-24 08:46:33