【问题标题】:Submit / Goto when focused in input and press enter专注于输入时提交/转到并按回车
【发布时间】:2013-02-24 03:35:34
【问题描述】:

我不确定为什么这不起作用?我尝试了几种方法并环顾四周,但没有一个示例对我有用。我的代码:

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

    }

});

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

【问题讨论】:

    标签: jquery submit goto enter


    【解决方案1】:

    收听keyup 事件,您缺少list.html 的引号,list 对象是undefined,所以它没有html 属性!

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

    【讨论】:

    • 我不知道为什么,但这仍然对我不起作用...让我添加我的 HTML,也许这会有所帮助?
    • @jstacks ID 必须是唯一的,jQuery 只选择具有该特定 ID 的第一个元素。
    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2011-05-10
    • 2021-10-25
    相关资源
    最近更新 更多