2

可能重复:
停止提交表单的 Enter/Return 键

我正在尝试使用我在网上找到的 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>
4

1 回答 1

2

您需要阻止表单在 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; }
于 2012-07-18T04:58:14.017 回答