OnkeyPress功能在引导程序输入框上不起作用



当用户将Enter键击中某个输入框时,我一直在尝试编码一个函数,该函数转到另一个页面。这是代码:

<div class="container">
        <div class="row">
            <form>
            <div id="search" onkeypress="enter(event)" >
                <div class="input-group col-md-offset-8 col-md-3">
                    <input type="text" class="form-control">
                    <div class="input-group-btn">
                        <a href="mylink.com" tab-index="-1" class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></a>
                    </div>
                </div>
            </div>
            </form>
        </div>
</div>
    <script>
            function enter(event){
                if (event.keyCode == 13){
                    document.location.href = "newpage.com";
                }
            }
    </script>

预先感谢!

正在发生的事情很简单,您有一个"表单"标签,因此,当您击中输入时," onkeypress"正在触发事件,而您创建的函数则正在处理该事件,但拥有"表格"标记它只是提交表格。

为了避免这种情况,只需删除"表单"标签,或者只需将其替换为以下内容:

<form onsubmit="event.preventDefault();">

另外,如果要重定向到其他页面,则必须添加协议,例如:

document.location.href = "http://newpage.com";

我希望它有帮助:)

相关内容

最新更新