在聚焦输入时提交/转到并按回车键

时间:2013-02-24 03:35:34

标签: jquery submit goto enter

我不确定为什么这不起作用?我尝试了几种方法并环顾四周,但这些例子都不适用于我。我的代码:

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

    }

});

感谢任何帮助。我哪里错了?

1 个答案:

答案 0 :(得分:0)

收听keyup事件,您遗漏了list.html的引号,list对象为undefined,因此它没有html属性!< / p>

$(function(){
    $('#SearchBar').keyup(function(event) {
        if (event.which == 13) {
            window.location.href = 'list.html'; 
        }
    });
})