【问题标题】:Custom CSS input box causing page to refresh on ENTER key [duplicate]自定义 CSS 输入框导致页面在 ENTER 键上刷新 [重复]
【发布时间】:2012-07-17 01:52:40
【问题描述】:

可能重复:
Stop Enter/Return key submitting a form

我正在尝试使用我在网上找到的 css 创建一个简单的搜索表单。我希望用户能够在文本框中点击 ENTER 键并让它运行查询,而无需用户单击按钮。我尝试检查 keydown 上的 ENTER 键,但由于某种原因页面不断刷新。这次刷新的原因是什么?

.searchform {
    display: inline-block;
    zoom: 1; /* ie7 hack for display:inline-block */
    *display: inline;
    border: solid 1px #d2d2d2;
    padding: 3px 5px;

    -webkit-border-radius: 2em;
    -moz-border-radius: 2em;
    border-radius: 2em;

    -webkit-box-shadow: 0 1px 0px rgba(0,0,0,.1);
    -moz-box-shadow: 0 1px 0px rgba(0,0,0,.1);
    box-shadow: 0 1px 0px rgba(0,0,0,.1);

    background: #f1f1f1;
    background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#ededed));
    background: -moz-linear-gradient(top,  #fff,  #ededed);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed'); /* ie7 */
    -ms-filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed'); /* ie8 */
}
.searchform input {
    font: normal 12px/100% Arial, Helvetica, sans-serif;
}
.searchform .searchfield {
    background: #fff;
    padding: 6px 6px 6px 8px;
    width: 202px;
    border: solid 1px #bcbbbb;
    outline: none;

    -webkit-border-radius: 2em;
    -moz-border-radius: 2em;
    border-radius: 2em;

    -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.2);
    -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.2);
    box-shadow: inset 0 1px 2px rgba(0,0,0,.2);
}
.searchform .searchbutton {
    color: #fff;
    border: solid 1px #494949;
    font-size: 11px;
    height: 27px;
    width: 35px;
    text-shadow: 0 1px 1px rgba(0,0,0,.6);

    -webkit-border-radius: 2em;
    -moz-border-radius: 2em;
    border-radius: 2em;

    background: #5f5f5f;
    background: -webkit-gradient(linear, left top, left bottom, from(#9e9e9e), to(#454545));
    background: -moz-linear-gradient(top,  #9e9e9e,  #454545);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#9e9e9e', endColorstr='#454545'); /* ie7 */
    -ms-filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#9e9e9e', endColorstr='#454545'); /* ie8 */
}

#search {margin-top:30px; marin-right:auto;}

采用以下形式:

<div align="center" id="search">
    <form class="searchform">
        <input class="searchfield" style="color:#636363" type="text" value="Title/Album..." onkeydown="if (event.keyCode == 13) {querySong();}" onfocus="if (this.value == 'Title/Album...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Title/Album...';}" />
    </form>
    <form class="searchform">
        <input class="searchfield" style="color:#636363" type="text" value="Artist..." onkeydown="if (event.keyCode == 13) {querySong();}" onfocus="if (this.value == 'Artist...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Artist...';}" />
        <input class="searchbutton" type="button" value="Song" onclick="querySong()"/>&nbsp
        <input class="searchbutton" type="button" value="Album" onclick="queryAlbum()"/>
    </form>
</div>

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您需要阻止表单在 Enter 上执行默认操作,即提交表单。

    这有点笨拙,你需要为你的情况选择合适的形式,但基本上你只需要返回 false 来防止默认行为。

    $('form').submit( function() { return false; } );
    

    或者没有 jQuery

    var el = document.getElementById('formId');
    
    if (el.addEventListener) {
      el.addEventListener('submit', preventDefaultAction, false); 
    } else if (el.attachEvent)  {
      el.attachEvent('submit', preventDefaultAction);
    }
    
    var preventDefaultAction = function () { return false; }
    

    【讨论】:

    • 如果没有 jquery,你会怎么做?
    • 用非 jQuery 方法更新了答案。
    • 嗯,好像没什么效果。
    • 您是否给表单提供了 ID &lt;form id='foo'&gt;,然后更改代码中的第一行以查找该 ID?
    • 确保在 onkeydownonclick 处理程序上也返回 false,我没有注意到您之前有这些(或者只是摆脱它们并在 Javascript 代码中处理) .一般来说,在 Javascript 中挂钩事件而不是使用这些事件是一种更好的做法。内联 Javascript 不再是最佳实践。
    猜你喜欢
    • 2015-04-28
    • 2014-02-03
    • 1970-01-01
    • 2013-02-15
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    相关资源
    最近更新 更多